使用 max() 获取多行
Fetch multiple rows using max()
我写了下面的查询,它给了我结果集 -
select
a.character_name,
b.planet_name,
sum(b.departure - b.arrival) AS screen_time
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name;
结果集:
Character_name | planet_name | Screen_time
C-3 PO Bespin 4
C-3 PO Hoth 2
C-3 PO Tatooine 4
Chewbacca Bespin 4
Chewbacca Endor 5
Chewbacca Hoth 2
Chewbacca Tatooine 4
现在,我如何 select 每个具有最大值 (screen_time) 的字符的 planet_name 和 character_name。
例如,
对于 C-3 PO,要显示两行
C-3 PO | Bespin
C-3 PO | Tattoine
对于 Chewbacca,显示一行
Chewbacca | Endor
我面临的问题是因为我无法执行到中间的条件table。
编辑(删除了我之前的回答)
您也可以使用 HAVING
子句中的相关子查询实现相同的目的:
select
a.character_name,
b.planet_name,
sum(b.departure - b.arrival) as screen_time
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name
having sum(b.departure - b.arrival) = (
select
max(tmp.screen_time)
from (
select b.character_name, sum(b.departure - b.arrival) as screen_time
from timetable b
group by b.character_name, b.planet_name) tmp
where a.character_name = tmp.character_name
group by tmp.character_name);
鉴于你的问题,我不确定你是想检索 screen_time
还是只检索 character_name
和 planet_name
。如果您只想要最后两列,请从主查询中删除 sum(b.departure - b.arrival) as screen_time
:
select
a.character_name,
b.planet_name
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name
having sum(b.departure - b.arrival) = (
select
max(tmp.screen_time)
from (
select b.character_name, sum(b.departure - b.arrival) as screen_time
from timetable b
group by b.character_name, b.planet_name) tmp
where a.character_name = tmp.character_name
group by tmp.character_name);
PS:我之前的回答没有用。抱歉。
select character_name, planet_name
from (
select
a.character_name,
b.planet_name,
sum(b.departure - b.arrival) AS screen_time
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name
) sq
where screen_time = (SELECT MAX(ssq.screen_time) FROM (
select
a.character_name,
b.planet_name,
sum(b.departure - b.arrival) AS screen_time
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name
) ssq WHERE ssq.character_name = sq.character_name
);
- 看到它在 sqlfiddle
中实时运行
不过别担心,性能应该不会像乍看起来那么糟糕。 MySQL often 足够聪明,不会执行同一个查询两次。
还有其他方法可以达到同样的目的。如果你愿意,你也可以试试这些:The Rows Holding the Group-wise Maximum of a Certain Column
我写了下面的查询,它给了我结果集 -
select
a.character_name,
b.planet_name,
sum(b.departure - b.arrival) AS screen_time
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name;
结果集:
Character_name | planet_name | Screen_time
C-3 PO Bespin 4
C-3 PO Hoth 2
C-3 PO Tatooine 4
Chewbacca Bespin 4
Chewbacca Endor 5
Chewbacca Hoth 2
Chewbacca Tatooine 4
现在,我如何 select 每个具有最大值 (screen_time) 的字符的 planet_name 和 character_name。 例如, 对于 C-3 PO,要显示两行
C-3 PO | Bespin
C-3 PO | Tattoine
对于 Chewbacca,显示一行
Chewbacca | Endor
我面临的问题是因为我无法执行到中间的条件table。
编辑(删除了我之前的回答)
您也可以使用 HAVING
子句中的相关子查询实现相同的目的:
select
a.character_name,
b.planet_name,
sum(b.departure - b.arrival) as screen_time
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name
having sum(b.departure - b.arrival) = (
select
max(tmp.screen_time)
from (
select b.character_name, sum(b.departure - b.arrival) as screen_time
from timetable b
group by b.character_name, b.planet_name) tmp
where a.character_name = tmp.character_name
group by tmp.character_name);
鉴于你的问题,我不确定你是想检索 screen_time
还是只检索 character_name
和 planet_name
。如果您只想要最后两列,请从主查询中删除 sum(b.departure - b.arrival) as screen_time
:
select
a.character_name,
b.planet_name
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name
having sum(b.departure - b.arrival) = (
select
max(tmp.screen_time)
from (
select b.character_name, sum(b.departure - b.arrival) as screen_time
from timetable b
group by b.character_name, b.planet_name) tmp
where a.character_name = tmp.character_name
group by tmp.character_name);
PS:我之前的回答没有用。抱歉。
select character_name, planet_name
from (
select
a.character_name,
b.planet_name,
sum(b.departure - b.arrival) AS screen_time
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name
) sq
where screen_time = (SELECT MAX(ssq.screen_time) FROM (
select
a.character_name,
b.planet_name,
sum(b.departure - b.arrival) AS screen_time
from characters a
inner join timetable b
on a.character_name = b.character_name
group by a.character_name, b.planet_name
) ssq WHERE ssq.character_name = sq.character_name
);
- 看到它在 sqlfiddle 中实时运行
不过别担心,性能应该不会像乍看起来那么糟糕。 MySQL often 足够聪明,不会执行同一个查询两次。
还有其他方法可以达到同样的目的。如果你愿意,你也可以试试这些:The Rows Holding the Group-wise Maximum of a Certain Column