【问题标题】:How to find the actor who has acted in the maximum number of movie and 3rd busiest actor如何找到演过最多电影的演员和第三繁忙的演员
【发布时间】:2020-06-21 08:54:36
【问题描述】:

有两张桌子

表 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


【解决方案1】:

我尝试了以下方法来找出谁在最大数量的电影中表演过。

您的原始查询非常接近 - 您只需要正确的 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 个最繁忙的演员,那么一种选择是使用窗口函数:

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

【讨论】:

    【解决方案2】:

    你可以做聚合: :

    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
                       );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-24
      • 2020-08-03
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多