【发布时间】: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