MySql遇到的问题--group by报错“1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains..”

学习group by遇到的问题

表,id为主键

±—±---------±----------±--------------------±-----------+
| id | LastName | FirstName | Address | City |
±—±---------±----------±--------------------±-----------+
| 1 | Adams | John | Oxford Street | London |
| 2 | Bush | George | Fifth Avenue | New York |
| 3 | Carter | Thomas | Changan Street | Beijing |
| 4 | Obama | Barack | Pennsylvania Avenue | Washington |
±—±---------±----------±--------------------±-----------+
1、

select id,Address,City,FirstName from people GROUP BY City;   

报错信息
select id,Address,City,FirstName from people GROUP BY City

1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘liuqing.people.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
查询时间: 0s

2、

select Address,City,FirstName from people GROUP BY City,FirstName,Address;  

结果:
结果可以运行成功
3、

select Address,City,FirstName from people GROUP BY id;  

结果:
在这里插入图片描述
4、

select id,Address,City,FirstName from people GROUP BY id,Address,City,FirstName;   

结果:在这里插入图片描述

通过对比以上4种结果,发现要想group by可以分组就要
1、使其前面的字段和后面的字段都有
2、group by后面主键’id‘
3、前后都有主键字段值
满足以上三个任意,才会支持sql语句group by的条件。
注意,第一个语句的报错sql_mode=only_full_group_by,需要满足sql的规定:

select @@GLOBAL.sql_mode;
select @@SESSION.sql_mode;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值