首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel DB::raw

Laravel DB::raw
EN

Stack Overflow用户
提问于 2018-05-14 14:55:33
回答 1查看 244关注 0票数 2
代码语言:javascript
复制
   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();

ERRor

代码语言:javascript
复制
"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。你能帮我吗?非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-14 14:57:51

having子句试试这个

代码语言:javascript
复制
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子句

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50333364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档