Laravel DB :: raw

时间:2018-05-14 14:55:33

标签: database laravel laravel-5.6

   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。你能帮助我吗???非常感谢

1 个答案:

答案 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子句