iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘xx
  • 308
分享到

MySQL报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘xx

mysql数据库sql 2023-09-16 19:09:16 308人浏览 泡泡鱼
摘要

一、完整报错内容 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregate

一、完整报错内容

1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘test.test_baobiao.qty’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by, Time: 0.000000s

在这里插入图片描述

二、原因分析

1、表描述

1> 表结构
在这里插入图片描述
2> sql语句

select Qty,businessDate,flag, sum(IF(flag=1, 0 - reverseQty, qty)) as sum from test_baobiao GROUP BY tradeOrderLineBO, businessDate;

3> 返回的报错信息

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

2、分析

从报错信息来看,它告诉我们:select字段里包含了没有被group by 条件唯一确定的字段qty

因为执行sql_make_group语句时会把两行纪录id=1和id=2两行记录合并成一行,但是搜索引擎不知道该返回哪一条,所以报错。

3、原因总结

Mysql 5.7.5后only_full_group_by成为sql_mode的默认选项之一,这可能导致一些sql语句失效。我们只需要把only_full_group_by从sql_mode中移除即可。

三、解决方案

1、直接在mysql-cli层面做设置

1> 先查询出所有的sql_mode

select @@global.sql_mode;

在这里插入图片描述

2> 将ONLY_FULL_GROUP_BY从sql_mode中移除:

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'

在这里插入图片描述
3> 重启MySQL客户端

如果此种方法无效(博主在navicat上操作有效),采用方法二、直接再MySQL-server侧修改sql_mode的配置

二、在MySQL-server层面修改sql_mode

vi /etc/mysql/conf.d/mysql.cnf

在配置文件my.cnf中关闭sql_mode=ONLY_FULL_GROUP_BY。
将sql_mode配置放在 [mysqld] 最后,如下:

[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

wq保存文件并退出,最后重启MySQL即可。

service mysql restart

四、效果

在这里插入图片描述

来源地址:https://blog.csdn.net/Saintmm/article/details/124491860

您可能感兴趣的文档:

--结束END--

本文标题: MySQL报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘xx

本文链接: https://www.lsjlt.com/news/410280.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作