Mysql遇到的坑:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

Mysql遇到的坑:Expression #1 of SELECT list is not in GROUP BY

clause and contains nonaggre

问题:ERROR 1055 (42000): Expression #7 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'postscan.verifyDelayLog.auditor' which is not functionally dependent on columns in GROUP BY clause; this is incompatible withsql_mode=only_full_group_by

原因: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参考手册”。)

解决方法:(二种方法,本人推荐方法二)

解决方法一:(此方法治标不治本,有一个弊端就是每次服务器重启,就会恢复默认设置,就需要再次执行命令。)

打开navcat,

1.用sql查询:

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

 

Mysql遇到的坑:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

2.用sql查询:

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

去掉ONLY_FULL_GROUP_BY,重新设置值。

Mysql遇到的坑:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

3.再执行set @@global.sql_mode,查看一下有没有去掉ONLY_FULL_GROUP_BY,结果去掉了。

Mysql遇到的坑:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

注意事项:

1.执行sql语句报错:variable ‘sql_mode’ can’t be set to the value of ‘NO_AUTO_CREATE_USER’
原因以及解决:8.0以上已经取消了NO_AUTO_CREATE_USER这个关键字,删掉sql语句中的这个关键字即可

Mysql遇到的坑:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

方法二:(一劳永逸的方法)

修改配置文件:

1.找到MySQL安装目录下,找对my.ini(windows系统)或者my.cnf(linux系统)配置文件,并打开;

2.添加如下代码:(注意:代码一定要放在[mysqld]节点最后一行,别放错了哦!)sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

3.保存配置文件,重启服务器。(一定要重启MySQL服务器哦!)

(本人是在windows系统下亲测有效,linux没试)

Mysql遇到的坑:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

至此问题已经解决!