MySQL中的GROUP BY获取其他字段方法

详细版本见个人博客:MySQL中的GROUP BY获取其他字段方法


创建测试数据

CREATE TABLE语句用于创建表,在test数据库下面创建一张名为show_plan的表:

create table show_plan(
id int primary key auto_increment,
name varchar(255),
desp int,
price int
);

INSERT INTO 语句用于向表格中插入新的行,现在我们向show_plan表中插入一些测试数据:

insert into show_plan(name,desp,price)
values ('a',1,55),
('b',1,20),
('c',1,63),
('b',2,89),
('c',2,78),
('a',2,90),
('b',3,88),
('c',3,77),
('a',3,100),
('d',1,77),
('e',4,75);
+----+------+------+-------+
| id | name | desp | price |
+----+------+------+-------+
|  1 | a    |    1 |    55 |
|  2 | b    |    1 |    20 |
|  3 | c    |    1 |    63 |
|  4 | b    |    2 |    89 |
|  5 | c    |    2 |    78 |
|  6 | a    |    2 |    90 |
|  7 | b    |    3 |    88 |
|  8 | c    |    3 |    77 |
|  9 | a    |    3 |   100 |
| 10 | d    |    1 |    77 |
| 11 | e    |    4 |    75 |
+----+------+------+-------+

问题

查询出每个演出单位票房最高的剧目名称。

分析与解答

一个典型的错误做法

select name,desp,MAX(price)
from show_plan
group by desp;

注意:除聚集计算语句外,SELECT语句中的每个列都必须在GROUP BY子句中给出

这里的select的name列并不在group by子句中。

MySQL也会报出如下错误:

ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shows.show_plan.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

正确做法1

select a.name,a.desp,b.price
from show_plan as a,(
    select desp,MAX(price) as price
    from show_plan
    group by desp
) as b
where a.price = b.price AND a.desp = b.desp;
+------+------+-------+
| name | desp | price |
+------+------+-------+
| a    |    2 |    90 |
| a    |    3 |   100 |
| d    |    1 |    77 |
| e    |    4 |    75 |
+------+------+-------+

如果只用price去匹配,假如存在有多个price相同时,

例如,desp=1的MAX(price)=77,但是desp=2中也含有price=77的行,那么就会被错误的选择出来。

所以要加一个AND a.desp = b.desp筛选条件。

正确做法2

select a.name,a.desp,a.price
from show_plan as a
where a.price in (
    select MAX(b.price)
    from show_plan as b
    where b.desp = a.desp
    group by b.desp
);
+------+------+-------+
| name | desp | price |
+------+------+-------+
| a    |    2 |    90 |
| a    |    3 |   100 |
| d    |    1 |    77 |
| e    |    4 |    75 |
+------+------+-------+

同样的,类似方法1,这里最关键的是where b.desp = a.desp这个筛选条件。

参考:关于group by 和max函数一起使用的坑


详细版本见个人博客:MySQL中的GROUP BY获取其他字段方法

  • 10
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
MySQL,使用GROUP BY语句进行分组查询时,查询的字段必须要么是分组字段,要么是聚合函数的参数。如果查询的字段既不是分组字段,也不是聚合函数的参数,那么在其他数据库执行时可能会报错。但是MySQL对此做了特殊处理,允许在GROUP BY语句返回其他字段而不报错。这种特殊处理是通过使用聚合函数来实现的,例如在你提供的第二个引用,使用了any_value函数来返回value字段的值。这样做可以确保查询结果的正确性,并且不会触发错误。所以在MySQL,你可以在GROUP BY语句返回其他字段而不会出错。 #### 引用[.reference_title] - *1* *2* [mysqlgroup by“的字段与“select“的字段不一致为什么不会出错?](https://blog.csdn.net/qq_41468822/article/details/130370347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [mysql group by 和select 列字段数不同(包括后续解决方法)](https://blog.csdn.net/song_chengbo/article/details/105393004)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值