likes
comments
collection
share

1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'xxxx'; this is incompatible with sql_mode=only_full_group_by

作者站长头像
站长
· 阅读数 79

在使用MySQL开发时可能会遇到以下报错:

1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'xxxx'; this is incompatible with sql_mode=only_full_group_by

原因:

在MySQL5.7.5后,默认开启了ONLY_FULL_GROUP_BY,所以导致了之前的一些SQL无法正常执行,其实,是我们的SQL不规范造成的,因为group by 之后,返回的一些数据是不确定的,所以才会出现这个错误。

在不使用group by 子句的聚合查询中,Select列表中的第一个表达式包含了非聚合的列**(改为聚会函数形成就可以正常执行了);当sql_mode为only_full_group_by时,是不能出现这种情况的。 也就是说,mysql的sql_mode是only_full_group_by的时候,在不使用group by 并且select后面出现聚集函数的话,那么所有被select的都应该是聚集函数,否则就会报错

root用户下,直接修改mysql.conf

查询mysql安装文件:whereis mysql 编辑/etc/my.cnf文件,加入如下参数:

sql_mode="NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES";

或直接中终端使用命令:

set sql_mode="NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES";

腾讯云解决方案:

仅选中NO_ENGINE_SUBSTITUTION、STRICT_TRANS_TABLES即可

1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'xxxx'; this is incompatible with sql_mode=only_full_group_by

1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'xxxx'; this is incompatible with sql_mode=only_full_group_by