欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

idea报错 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

程序员文章站 2022-07-15 12:26:46
...

idea报错 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre 如何解决

写了一个查询语句执行的时候,idea日志报错

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
在Navicat中执行也会报这个错,查阅了资料,发现是sql_mode的错误

有两种方法解决,其实都是一个操作

方法一(在服务器上直接更改):

在服务器中查询sql_mode

show variables like 'sql_mode';

idea报错 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
再重新set sql_mode

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

idea报错 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

方法二(使用Navicat更改):

1.首先也是在Navicat中查询

select @@global.sql_mode

idea报错 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
2.更改sql_mode

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

idea报错 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
3.重新使用第一步的查询语句,就发现sql_mode已经更改了

总结:其实这两种方法都是通过修改sql_mode来解决方法了,所以其实是一种。

这个问题出现的原因是因为MySQL 5.7.5及以上功能依赖检测功能。如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDER BY列表的查询引用在GROUP BY子句中既未命名的非集合列,也不在功能上依赖于它们。(5.7.5之前,MySQL没有检测到功能依赖关系,默认情况下不启用ONLY_FULL_GROUP_BY。有关5.7.5之前的行为的说明,请参见“MySQL 5.6参考手册”。)