mysql常见报错之SELECT list is not in GROUP BY clause

概述

记一次最近碰到的一个mysql报错,还是比较少见的。

1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'information_schema.STATISTICS.NON_UNIQUE' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

mysql常见报错之SELECT list is not in GROUP BY clause


思路:

MySQL 5.7.5及以上功能依赖检测功能。如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDER BY列表的查询引用在GROUP BY子句中既未命名的非集合列,也不在功能上依赖于它们。(5.7.5之前,MySQL没有检测到功能依赖关系,默认情况下不启用ONLY_FULL_GROUP_BY。)


解决方法:

1、查询sql_mode

select @@global.sql_mode

查询出来的值为:ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

mysql常见报错之SELECT list is not in GROUP BY clause


2、去掉ONLY_FULL_GROUP_BY,重新设置值。

set @@global.sql_mode 
='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

mysql常见报错之SELECT list is not in GROUP BY clause

或者直接修改my.cnf文件,添加

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO


3、测试

mysql常见报错之SELECT list is not in GROUP BY clause

测试成功。


一般情况下还是不建议调整这个sql_mode,要看下这个SQL到底哪个地方不是很规范...觉得有用的朋友多帮忙转发哦!后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注下~

mysql常见报错之SELECT list is not in GROUP BY clause

相关推荐