当前位置:   article > 正文

mysql报错:Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column

expression #2 of select list is not in group by clause and contains nonaggre

起因:

今天把数据从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

结果:

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

如果看到有ONLY_FU

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/899521
推荐阅读
相关标签
  

闽ICP备14008679号