【问题标题】:How to find which actor has participated in the most films in a single year如何查找一年内参演电影最多的演员
【发布时间】:2020-08-03 20:58:40
【问题描述】:

我得到了一个包含以下表格的 IMDB 数据库:

movies    
    +-------+
    | Field | 
    +-------+
    | id    | 
    | title | 
    | year  | 
    | genre | 
    +-------+

actors
+-----------+
| Field     |
+-----------+
| id        | 
| full_name | 
| gender    |
+-----------+
cast
+--------
| Field    | 
+----------+
| actor_id | 
| movie_id | 
| salary   |
+----------+

我正在寻找一年内参演电影最多的演员。我试过了

select full_name
     , count(title)
     , year 
  from actors 
  join cast 
    on cast.actor_id = actors.id 
  join movies 
    on movies.id = cast.movie_id 
 group 
    by year 
 order 
    by count(title)

这就是我得到的

+--------------------+--------------+------+
| full_name          | count(title) | year |
+--------------------+--------------+------+
| Abraham Aronofsky  |           28 | 1998 |
| William Armstrong  |           30 | 1986 |
| Kevin Bacon        |           39 | 1984 |
| J. Todd Anderson   |           40 | 1996 |
| Kevin Bacon        |           43 | 1978 |
| Kevin Bacon        |           49 | 1987 |
| Rudy Bond          |           54 | 1972 |
| Kevin Bacon        |           59 | 1992 |
| Dean Alexandrou    |           62 | 2005 |
| Geoffrey Arend     |           79 | 2004 |
| Billy Dee Williams |           93 | 1983 |
| Charles Adler      |           98 | 1989 |
| Graham Ashley      |          104 | 1977 |
| Carl Allen         |          110 | 1994 |
| Lewis Abernathy    |          130 | 1997 |
| Steve Altes        |          149 | 2000 |
| van Allen          |          157 | 1995 |
| David Andrews      |          162 | 1999 |
| Michael Bowen      |          173 | 2003 |
| Casey Affleck      |          193 | 2001 |
| Henri Alciatore    |          230 | 1991 |
+--------------------+--------------+------+

但我怀疑这只是列出了在给定年份参与任何电影的演员数量,而不是我想要完成的目标。有任何想法吗?

【问题讨论】:

  • 尝试SQL的max()函数你会得到你想要的结果
  • 奇怪的是convert不是MySQL中的保留字,而是CAST甚至没有被列为关键字

标签: mysql sql group-by count greatest-n-per-group


【解决方案1】:

使用 HAVING 和 MAX 函数在下面试试这个:

select full_name, count(title), year 
from actors 
join cast on cast.actor_id=actors.id 
join movies on movies.id=cast.movie_id 
group by year having count(title)=( 
select max(title_count) 
from ( 
full_name, count(title) title_count, year 
from actors 
join cast on cast.actor_id=actors.id 
join movies on movies.id=cast.movie_id 
group by year));

【讨论】:

  • 非常感谢,但这会给出错误 'count(title) title_count, year from actor join cast on cast.actor_id=actors.i'
【解决方案2】:

如果您运行的是 MySQL 8.0,您可以使用聚合来计算每个演员每年投了多少部电影,并使用窗口函数 rank() 来识别最活跃的演员(包括关系):

select *
from (
    select 
        a.full_name, 
        m.year, 
        count(*) no_movies, 
        rank() over(partition by m.year order by count(*) desc) rn
    from actors a
    inner join cast c on c.actor_id = a.id
    inner join movie m on m.id = c.movie_id
    group by a.id, a.full_name, m.year
) t
where rn = 1
order by year

在早期版本中,它有点复杂。一种选择使用相关子查询进行过滤:

select 
    a.full_name, 
    m.year, 
    count(*) no_movies
from actors a
inner join cast c on c.actor_id = a.id
inner join movie m on m.id = c.movie_id
group by a.id, a.full_name, m.year
having count(*) = (
    select count(*)
    from cast c1
    inner join movie m1 on m1.id = c1.movie_id
    where c1.year = c.year
    group by c.actor_id
    order by count(*) desc
    limit 1
)

【讨论】:

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