Loading

mysql 8.0下的SELECT list is not in GROUP BY clause and contains nonaggregated column

查看mysql的版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.12 |
+-----------+

在执行group  by时遇到报错,具体如下

mysql> select * from api_properties GROUP BY file_id order by file_id;
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'bim.api_properties.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

记得上次在5.7下也遇到了同样的问题  5.7的

win下修改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

重启mysql

解决方法

方法一
使用命令行或者数据库客户端执行SQL
1.SQL语句,select @@global.sql_mode查询

mysql> select @@global.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@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 |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.06 sec)

2.去掉ONLY_FULL_GROUP_BY,重新设置值

mysql> 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';

方法二
vi修改MySQL配置文件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服务:service mysql restart

posted @ 2022-08-28 18:42  LiJialong  阅读(108)  评论(0编辑  收藏  举报