gpt4 book ai didi

sql - 选择播放电影次数最多的 Actor

转载 作者:行者123 更新时间:2023-12-05 08:33:23 30 4
gpt4 key购买 nike

我有 2 个表。1)dbo.movi​​es

+------------+-------------+------------+
| movie_id | movie_name | actor_id |
+------------+-------------+------------+
| 1 | name1 | 1 |
| 2 | name2 | 1 |
| 3 | name3 | 4 |
| 4 | name4 | 2 |
| 5 | name5 | 1 |
| 6 | name6 | 5 |
| 7 | name7 | 3 |
+------------+-------------+------------+

2) dbo.actors

+------------+-------------+
| actor_id | actor_name |
+------------+-------------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
+------------+-------------+

我想SELECT在电影中播放次数最多的actor_idactor_name

虽然我知道如何找到一个 Actor 演过最多的电影,但我找不到那个 Actor 是谁:

select max(y.x)
from (select count(actor_id) as x from movies group by actor_id) y

最佳答案

加入actors表获取姓名,按个数排序,只取第一条记录

select top 1 a.actor_name, count(*) as cnt
from actors a
join movies m on a.actor_id = m.actor_id
group by a.actor_id, a.actor_name
order by count(*) desc

如果 2 位 Actor 出演了相同数量的电影,而我想同时获得他们两个,该怎么办?

一种方法是

select a.actor_name, count(*) as cnt
from actors a
join movies m on a.actor_id = m.actor_id
group by a.actor_id, a.actor_name
having count(*) = (select top 1 count(*) max_cnt from movies group by actor_id order by count(*) desc)

关于sql - 选择播放电影次数最多的 Actor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41298496/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com