Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
原文链接:https://blog.csdn.net/hq091117/article/details/79065199
https://blog.csdn.net/allen_tsang/article/details/54892046
MySQL 5.7.5后only_full_group_by成为sql_mode的默认选项之一,这可能导致一些sql语句失效。
比如下表game:
| id | group_id | name | score |
|---|---|---|---|
| 1 | A | 小刚 | 20 |
| 2 | B | 小明 | 19 |
| 3 | B | 小花 | 17 |
| 4 | C | 小红 | 18 |
执行sql:
select name, group_id from game group by group_id(记为sql_make_group)
返回:
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'game.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
解决方法
把group by字段
group_id设成primary key 或者 unique NOT NULL。这个方法在实际操作中没什么意义。使用函数
any_value把报错的字段name包含起来。如,select any_value(name), group_id from game group by group_id。在配置文件my.cnf中关闭sql_mode=ONLY_FULL_GROUP_BY.。msqyl的默认配置是
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。可以把ONLY_FULL_GROUP_BY去掉,也可以去掉所有选项设置成sql_mode=,如果你确信其他选项不会造成影响的话。
成功的步骤:
命令行打开mysql.cnf,可以用whereis查找
sudo vim /etc/mysql/conf.d/mysql.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
wq保存并退出
重启MySQL
sudo service mysql restart
执行成功后,返回结果应该是
| name | group_id |
|---|---|
| 小刚 | A |
| 小明 | B |
| 小红 | C |
为什么默认设置ONLY_FULL_GROUP_BY限制?
对于上述的报错信息,我的理解是select字段里包含了没有被group by条件唯一确定的字段name。
因为执行sql_make_group语句实际上把两行纪录小明和小花合并成一行,搜索引擎不知道该返回哪一条,所以认为这样的sql是武断的(arbitrary).
解决办法2和3都是禁止检查返回结果的唯一性。
进阶讨论
上述办法可以解决报错的问题,但也说明sql语句本身有逻辑问题。name字段不应该出现在返回结果,因为它是不确定的。
考虑这样的需求:按group_id分组后,找出每组得分最少的人。
执行sql: select any_value(name) as name, group_id, min(score) as score from game group by group_id order by min(score)
返回结果
| name | group_id | score |
|---|---|---|
| 小明 | B | 17 |
| 小红 | C | 18 |
| 小刚 | A | 20 |
B组的name是小明(因为小明的id更小),而期望结果应该是小花。
所以单纯使用group by无法实现这样的需求。可以使用临时表的方法:
select id, name,t.group_id, t.score from (select group_id, min(score) as score from game group by group_id order by min(score)) t inner join game on t.group_id=game.group_id and t.score=game.score
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的问题
报错信息 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'a.id' ...
- mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
问题出现的原因: MySQL 5.7.5及以上功能依赖检测功能.如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDER BY列 ...
- [mysql] Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'loser.tank_admin.login_ip' which is not functionally dependent on columns in GROUP BY clause; this is
执行SQL: SELECT login_name,login_ip,sex FROM tank_admin GROUP BY login_name ; 时抛出异常. Expression #2 of ...
- 【mybatis】【mysql】mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains no ...
- Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'blog.t_blog.addTime' which is not functi
sql报错: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #1 of SELECT ...
- 记录一次mysql由5.6升级到5.7出现的异常---Expression #23 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'c.commentCount' which is not functionally dependent on columns in GROUP BY clause;
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expre ...
- MYSQL报错:1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'rpa ...
- mysql升级后出现Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
安装了mysql5.7,用group by 查询时抛出如下异常: Expression #3 of SELECT list is not in GROUP BY clause and contains ...
- Error Code: 1055.Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
环境:mysql-8.0.15-winx64 问题描述: Error querying database. Cause: java.sql.SQLSyntaxErrorException: Expre ...
随机推荐
- CentOS7 线上环境的一些 配置
上周服务器被攻击导致上面收回了我们服务器的IP,所以这周重新安装部署了服务器,使用centos7系统.为了防止服务器再次被攻击,所以建议以下几点: 1. root密码要复杂一点,尽量字母数字特殊字符都 ...
- 实验吧CTF题库-密码学(部分)
这里没有key: 打开链接,有一个弹窗 然后就是一个空白网页,右键查看源代码 这里有一串js密文,解密一下,https://www.dheart.net/decode/index.php 得到flag ...
- oracle sql 函数
(7)查询日期之间的数据 例如查询student表中出生日期(birthday)在’2016-01-01’ 和’2017-01-01’之间的数据: select * from student wher ...
- Linux-CentOS 学习的坎坷路 (一) 网络配置篇
自己学习的地址:http://www.imooc.com/view/175 学到2.8章节,配置IP这一块,妈蛋,他直接跳过了,都不知道怎么配置,无奈,只能Search 先是找到配置IP的方法: ht ...
- python的文件锁使用
python的文件锁目前使用的是fcntl这个库,它实际上为 Unix上的ioctl,flock和fcntl 函数提供了一个接口. 1.fcntl库的简单使用 import fcntl import ...
- 关于handler的再次讨论
主要有两个问题,post方法和sendmessage方法有什么不同? 同一个handler对象发送的message只能发送给自己吗? 问题1: post方法,对于Handler的Post方式来说,它会 ...
- day17 13.滚动结果集介绍
滚动 一般结果集只能是向下的,不是滚动的,你要是想让它滚动你得设置才行. 类名或者接口里面有静态的可以.接口里面的属性全部都是public static final,类名/接口名.是属性,这些都是常量 ...
- GOOGLE机器学习速成班
地址:https://developers.google.cn/machine-learning/crash-course/ 不用***就可以学习.
- Qt嵌入式开发环境搭建
一.Qt版本介绍 按照不同的图形界面来划分,分为四个版本: 1.Win32版:适用于windows平台 2.X11版:适用于各种X系统的Linux和Unix平台 3.Mac版:适用于苹果的MacOS ...
- ZROI2018提高day5t2
传送门 分析 考场上傻了,写了个树剖还莫名weila...... 实际就是按顺序考虑每个点,然后从他往上找,一边走一边将走过的边染色,如果走到以前染过色的边就停下.对于每一个a[i]的答案就是之前走过 ...