SQL 查询报错:Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'xxx' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
场景大概如下:
有成绩表score, 字段有math,age.....等等,我现在要查询各个年龄学生数学成绩的总和并降序排序.
select sum(math) mathScore,
age
from score
group by age
order by math;
然后查询报错上述错误,由于当时脑子有点短路,实在没发现错哪里,就上网查了查,基本都是说怎么怎么改配置,甚至有的离谱到说group by中出现的字段order by中也要有!
于是我放弃了,该配置是不可能的,报错就改配置,那开发团队还怎么玩!所以一个重新看看报错信息,大概意思是order by 中的条件功能上依赖group by中的内容,而在order by 中没有group by后的结果和条件。然后在看查询,也算明白原因了。修改查询:
select sum(math) mathScore,
age
from score
group by age
order by mathScore;
报错问题解决,原因group by的结果是math的聚和,而order by却是用最初的字段排序。
低级低级!脑壳昏!