解决 mysql8 报错 this is incompatible with sql_mode = only_full_group_by

报错内容

Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column '{field}' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 0.053 sec

确认运行模式

mysql> select @@global.sql_mode;

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

重新设置运行模式

vi /etc/my.cnf
[mysqld]
# 去掉ONLY_FULL_GROUP_BY即可
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

重启服务

service mysqld restart

感谢阅读这篇文章,如果你喜欢,或者遇到了问题,可以关注我的公众号