起因:
今天把数据从mysql5.7.x 迁移到8.0.x版本的时候项目接口报错。最后发现是使用group by的sql语句时候发现mysql出现如下问题:
报错代码:
Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'oadbuat.a.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
问题产生原因:
经过查询资料发现group by 聚合操作的时候,进行查询操作的列没有在group by 中出现,那么这个sql就是不合法的。主要原因就是设置的与sql_mode=only_full_group_by不兼容。
通过查询发现mysql5.7.5及以上的版本都会依赖检测功能,也就是默认会开启only_full_group_by。
解决方案:
首先执行一下命令查看是否开启了only_full_group_by
select @@global.sql_mode
结果: