A、B 两个表。A 为主表。B 为附表。A 表 left join B(B 表按某一字段取最大分组)如何实现?

select count(*) as aggregate from `new_houses`
left join
(select max(open_date) as open_date, price, new_house_id from quotas group by new_house_id) as a
on `new_houses`.`id` = `a`.`new_house_id` 
where `is_lock` = 'F' and `city_id` = 1`

这个在sql里执行没问题,但是实际在代码里执行报错有人知道吗?
报错如下:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'house.quotas.price' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select count(*) as aggregate from new_houses left join (select max(open_date) as open_date, price, new_house_id from quotas group by new_house_id) as a on new_houses.id = a.new_house_id where is_lock = F and city_id = 2)

franktrue
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1

具体的话看这里
其实我是根据关键字去百度的。this is incompatible with sql_mode=only_full_group_by

你试试看可不可以。

摘抄部分:

看一下group by的语法:
select 选取分组中的列+聚合函数 from 表名称 group by 分组的列
从语法格式来看,是先有分组,再确定检索的列,检索的列只能在参加分组的列中选。
我当前Mysql版本5.7.17,
再看一下ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中,也就是说查出来的列必须在group by后面出现否则就会报错,或者这个字段出现在聚合函数里面。

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!