SELECT list is not in GROUP BY clause and contains nonaggregated column
问题:
SELECT list is not in GROUP BY clause and contains nonaggregated column
原因:
举个例子,有这么一个查询语句:
SELECT
user_name as name,
user_age as age
FROM
user
GROUP BY
user_age
在MySQL5.7之后,sql_mode中ONLY_FULL_GROUP_BY模式默认设置为打开状态,这样的话,你select(查询)的字段都要包含在group by中,比如例子中,根据user_age进行分组,但是你查询的时候包含了user_name和user_age两个字段,而user_name不包含在group by当中,所以就会报错。
比如,表中的数据是这样的:
user_name |
user_age |
小明 |
23 |
小东 |
23 |
当你使用group by通过user_age进行分组,那么分组结果只有23,但是23中user_name有小明和小东,那么究竟是返回(小明,23)还是(小东,23)就存在歧义了
除非把语句中的user_name去掉,如下所示:
SELECT
user_age as age
FROM
user
GROUP BY
user_age
那么如果要查询user_name和user_age两个字段,并且根据user_age分组应该怎么解决呢?解决方法如下。
解决方法:
一种方法是通过临时或者修改配置文件的方式去掉ONLY_FULL_GROUP_BY 模式,这里就不讲了。这里讲另一种方法:通过使用any_value()函数来解决。
把原来的语句改写成如下形式:
SELECT
any_value(user_name) as name,
user_age as age
FROM
user
GROUP BY
user_age
如果有查询更多字段,形式如下:
SELECT
any_value(user_name) as name,
any_value(user_id) as id,
user_age as age
FROM
user
GROUP BY
user_age
SELECT list is not in GROUP BY clause and contains nonaggregated column相关推荐
- Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
场景 安装了mysql5.7,用group by 查询时抛出如下异常: Expression #1 of SELECT list is not in GROUP BY clause and conta ...
- mysql 报错解决思考Expression #5 of SELECT list is not in GROUP BY clause and contains nonaggregated column
mysql报错: [Err] 1055 - Expression #5 of SELECT list is not in GROUP BY clause and contains nonaggrega ...
- 报错,> 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
错误: mysql: select * ,count(*)as count from app_cash_trans_log group by app_cash_trans_log.tran_code ...
- 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 ...
- 解决SELECT list is not in GROUP BY clause and contains nonaggregated column..
问题描述 在一次zabbix的mysql库中写联合查询语句时报错 1055 - Expression #1 of SELECT list is not in GROUP BY clause and c ...
- MySQL Expression 2 of SELECT list is not in GROUP BY clause and contains nonaggregated column yous
MySQL:Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yous ...
- 解决SELECT list is not in GROUP BY clause and contains nonaggregated column ‘XXX’
解决SELECT list is not in GROUP BY clause and contains nonaggregated column 'XXX' 我是在Linux上面新装了一个mysql ...
- mysql使用group by查询报错SELECT list is not in GROUP BY clause and contains nonaggregated column.
mysql使用group by查询报错SELECT list is not in GROUP BY clause and contains nonaggregated column. 出现原因 使用g ...
- MySQL报错 SELECT list is not in GROUP BY clause and contains nonaggregated column...
MySQL报错 SELECT list is not in GROUP BY clause and contains nonaggregated column- 原因: 在mysql5.7以上的版本中 ...
最新文章
- 如何用计算机完成一片文稿制作,怎样在家用电脑上制作幻灯片?
- PCL谢谢笔记 体素栅格滤波(下采样)
- 【机器学习】UMAP:强大的可视化降维工具
- Linux常用命令和服务器配置
- eclipse创建springboot项目_Spring Initializer+IntelliJ IDEA创建Spring Boot项目(图文)
- 老赵被刷票了,但这不是老赵做的
- (13)FPGA面试技能提升篇(Shell脚本)
- c语言中数组结尾的0的作用,C语言里面一个数组最后的\0表示什么意思?
- [轉載]Google C++編程風格指南(四):智能指針和其他C++特性
- oracle 存储过程记录
- ❤️再也不用为了重写方法而苦恼了,Lombok帮你解决!
- git add/commit/pull之间的关系
- java se 通用能力-判断推理专项练习(选择题)
- HTML:Boostarp设计登录页面
- 莫名其妙的解决了MagicDraw中文问题
- UML建模工具最近更新(-2022年4月)共12款:Papyrus、StarUML、Software Ideas Modeler
- 计算机平均分函数a,平均值计算函数Average、Averagea、AverageIfs、Trimmean
- 《代码整洁之道》--读书笔记
- 关于VMware ESX与VMware ESXi区别
- css中的div容器笔记