Trip::select('trips.id','trips.date_trip',
DB::raw('(select count(region_id) as count from trip_regions where trip_id=trips.id) as count')
)->where('count',10)->get();
错误
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in
'where clause' (SQL: select `trips`.`id`, `trips`.`date_trip`, (select
count(region_id) as sum from trip_regions where trip_id=trips.id) as
count from `trips` where `count` = 10)
我有查询行想要DB :: raw。你能帮助我吗???非常感谢
答案 0 :(得分:0)
使用having
子句
Trip::select('trips.id','trips.date_trip',
DB::raw('(select count(region_id) as count from trip_regions where trip_id=trips.id) as count')
)->having('count', '=',10)->get();
您无法对自定义别名应用where
子句,where
仅适用于您的表中存在的列。要过滤掉表达式/聚合结果集的结果,您需要having
子句