网络编程
位置:首页>> 网络编程>> 数据库>> MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

作者:.猫的树  发布时间:2024-01-15 17:25:21 

标签:mysql,报错,expression

项目场景:

最近在部署项目之后,运行出现报错:

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘grades.order_id’ 
which is not functionally dependent on columns in GROUP BY clause; 
this is incompatible with sql_mode=only_full_group_by

问题描述

字面意思理解:

SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且包含非聚合列“grades.order_id” 它在功能上不依赖于
GROUP BY 子句中的列; 这与 sql_mode=only_full_group_by 不兼容

使用GROUP BY 语句违背了sql_mode=only_full_group_by。因为mysql版本5.7之后默认的模式是ONLY_FULL_GROUP_BY

原因分析:

查看官方文档,发现从 MySQL 5.7.5 开始,默认 SQL 模式包括 ONLY_FULL_GROUP_BY。 (在 5.7.5 之前,MySQL 不检测函数依赖,并且默认不启用 ONLY_FULL_GROUP_BY。)这可能会导致一些sql语句失效。

解决方案:

执行命令vim /etc/mysql/conf.d/mysql.cnf修改配置文件

如果my.cnf中有sql_mode配置,则将ONLY_FULL_GROUP_BY去掉。

如果没有,则将下面内容放到相应位置

[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

保存之后,执行命令service mysql restart重启mysql即可。

2022.06.10更新

MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

最近用docker安装mysql又出现这个问题,修改配置文件之后,重启mysql发现连不上mysql

于是查看错误信息 docker logs -f --tail 10 mysql,出现报错:

Error while setting value
‘STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’
to ‘sql_mode’

MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

原来mysql 8.0 以上取消了NO_AUTO_CREATE_USER这个关键字,在my.cnf中的sql_mode中删掉这个关键字即可。

来源:https://blog.csdn.net/qq_34383510/article/details/124764673

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com