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)
关于 LearnKu
具体的话看这里
其实我是根据关键字去百度的。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后面出现否则就会报错,或者这个字段出现在聚合函数里面。