database - Laravel DB::raw
问题描述
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。你能帮助我吗???非常感谢
解决方案
试试这个 withhaving
子句
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
子句
推荐阅读
- python - 在 Pandas 中检查字符串是否存在于另一列的值中的最有效方法
- sql - 提高子查询中限制的性能
- postgresql - 在 SQuirreL 中输出 PostgreSQL 的位类型会导致“
" - node.js - 穆尔特 | 不断收到““路径”参数必须是字符串类型。收到的类型号“
- apache-spark - 无法在后续会话中读取持久化的 Spark 仓库数据库
- java - 调试时是否可以立即将 log4j2 消息刷新到控制台?
- python - 另存为 py 导致 500:内部服务器错误
- linux - 删除基于一个文件的行包含到另一个
- add - CLIPS - 添加两个事实的插槽
- python - 使用 Resemblyzer 进行电话交谈的说话者分类