mysql异常[Err] 1055 - GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

MySQL报错信息如下: 

Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column ‘数据库名.表名.字段名’ which is not functionally dependent
on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by

报错原因

MySQL5.7.5版本以上默认的sql配置是:sql_mode=“ONLY_FULL_GROUP_BY”,很多从5.6升级到5.7时,为了语法兼容,大部分都会选择调整sql_mode,使其保持跟5.6一致,为了尽量兼容程序。

由于开启了ONLY_FULL_GROUP_BY的设置,如果select 的字段不在 group by 中,并且select 的字段未使用聚合函数(SUM,AVG,MAX,MIN等)的话,那么这条sql查询是被mysql认为非法的,会报错误。

解决方案

查看sql_mode

select @@GLOBAL.sql_mode;

修改sql_mode

SET @@GLOBAL.sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

猜你喜欢

转载自blog.csdn.net/watson2017/article/details/132061649