最近的项目用到mysql8.0,在用group by的时候会报错

Error Code: 1055. Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘×××’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

查了下这是mysql5.7下mysql的model默认

ONLY_FULL_GROUP_BY
含义 sql中select后面的字段必须出现在group by后面,或者被聚合函数包裹

解决方法

1.直接修改my.ini/my.cnf (长期解决问题)

(1) Windows 系统 找到 my.ini Linux 系统 找到 my.cnf

sql_mode =STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

(2) 重启 Mysql 服务

net stop mysql
net start mysql

2.在命令行运行 (仅本次连接设置)

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

原帖地址:https://www.zmhit.com/article/71

最后修改:2022 年 06 月 01 日
如果觉得我的文章对你有用,请随意赞赏