i have the following query
select * from (SELECT accident.id,count(*) as cnt from accident left join driver on driver.accident_id = accident.id group by accident.id)alias where cnt = 1
and this is my query builder
$accidents = DB::table('accident')
->leftjoin('driver','accident.id','driver.accident_id')
->select(DB::raw('accident.*,count(*) as jumlah_kendaraan'))->groupBy('accident.id')
->where('jumlah_kendaraan', $jumlah_kendaraan);
i tried to convert it like the above but i got an error says
SQLSTATE[42703]: Undefined column: 7 ERROR: column "jumlah_kendaraan" does not exist
can anyone help me to solve it? thanks in advance
You cannot filter the result of an aggregate function count()
using where clause, Instead use having
for this purpose
->having('jumlah_kendaraan', $jumlah_kendaraan)
Or use your complete expression
->havingRaw('count(*) = '.$jumlah_kendaraan)
where clause
can not use alias of group function directly, use
->where('count(*)', $jumlah_kendaraan);
You can use having
as well but in case if having
is not working use like given above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.