sql_mode=only_full_group_by的mysql5.6升级5.7的问题
mysql5.7使用group by分组时,查询字段没有分组的字段就会报错
[Err] 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.user.id'
which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_byy
原因:
mysql5.7以上默认开启了only_full_group_by 模式
解决方法:
1、查看sql_mode
SELECT @@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
2、将查询的结果去掉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
在win系统找到mysql的my.ini文件,在文件中添加:
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
完整的my.ini文件内容
[[mysqld] port = 3306 basedir=D:/MySQL/mysql-5.7.24-winx64 datadir=D:/MySQL/mysql-5.7.24-winx64/data max_connections=200 character-set-server=utf8 default-storage-engine=INNODB sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION [mysql] default-character-set=utf8b
保存好后,重启mysql服务
Linux下修改mysql的my.cnf配置文件
找到my.cnf中的[mysqld]在内增加:
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
保存好后,重启mysql服务