Mysql8.0: SELECT list is not in GROUP BY clause and contains nonaggregated column

3030人浏览 2020-09-10

安装完mysql8.0,运行项目报错SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 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

解决方案:找到配置文件my.ini,在[mysqld]节点下添加如下配置

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

或者

sql_mode=NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION

如果已经有sql_mode 可以注释掉。之后重启mysql就可以了

 

错误原因:这个错误发生在mysql 5.7 版本及以上版本会出现的问题,在mysql5.7版本默认的sql配置是:sql_mode=”ONLY_FULL_GROUP_BY”,这个配置严格执行了”SQL92标准”,很多从5.6升级到5.7时,为了语法兼容,大部分都会选择调整sql_mode,使其保持跟5.6一致,为了尽量兼容程序。

推荐文章

MySQL千万级数据数据库设计优化解决方案
2021-03-11
Mysql Group by 后取最新插入的一条数据。
2021-11-08
Mysql 查询时使用GROUP BY 后字段排序问题,需要保留group by 后某个字段最大的那条数据
2021-01-25
搜索文章