如何找到在最大电影数量和第三繁忙的演员中扮演过的演员

问题描述 投票:0回答:2

获得2张桌子

enter image description here

表1-ACTOR:ACTOR_ID,FIRST_NAME,LAST_NAME,LAST_UPDATE

表2-FILM_ACTOR:ACTOR_ID,FILM_ID,LAST_UPDATE

我尝试了以下方法来找出谁参加了最多的电影演出

select top 1 concat(ACTOR.FIRST_NAME, ACTOR.LAST_NAME) as Full_name
from ACTOR
left join FILM_ACTOR on ACTOR.ACTOR_ID = FILM_ACTOR.ACTOR_ID
group by FILM_ACTOR.ACTOR_ID
order by Full_name desc;
mysql sql group-by count greatest-n-per-group
2个回答
0
投票

您可以进行聚合::

select a.*
from actor a 
where a.actor_id = (select top (1) fm.actor_id 
                    from film_actor fm 
                    group by fm.actor_id 
                    order by count(*) desc
                   );

0
投票

我尝试了以下方法,以找出谁曾参加过最多的电影。

您的原始查询非常接近-您只需要适当的order by子句:

select top (1) concat(a.first_name, a.last_name) as full_name
from actor a
left join film_actor fa on a.actor_id = fa.actor_id
group by a.actor_id, a.first_name, a.last_name
order by count(*) desc;

如果要nth最忙的actor,则一种选择是使用窗口函数:

select full_name
from (
    select concat(a.first_name, a.last_name) as full_name, row_number() over(order by count(*) desc) rn
    from actor a
    left join film_actor fa on a.actor_id = fa.actor_id
    group by a.actor_id, a.first_name, a.last_name
) t
where rn = 3
© www.soinside.com 2019 - 2024. All rights reserved.