[英]Laravel query builder “where”
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 你不能过滤的聚集函数的结果
count()
使用where子句,而是使用having
用于此目的
->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 clause
不能直接使用组函数的别名,请使用
->where('count(*)', $jumlah_kendaraan);
You can use having
as well but in case if having
is not working use like given above. 您也可以使用
having
,但如果如上所述无法使用having
则可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.