我想找到每种类型的电影,找到在大多数类型的电影中播放的N个演员
表及其列:
actor(actor_id,name)
role(actor_id,movie_id)
movie(movie_id,title)
movie_has_genre(movie_id,genre_id)
genre(genre_id,genre_name)
我开始这样做了:
select genre.genre_name,actor.actor_id,count(genre.genre_name) as max_value from genre
inner join movie_has_genre on movie_has_genre.genre_id=genre.genre_id
inner join movie on movie_has_genre.movie_id=movie.movie_id
inner join role on movie.movie_id=role.movie_id
inner join actor on actor.actor_id=role.actor_id
group by genre.genre_name,actor.actor_id
order by max_value desc;
这给了我各种类型的电影,演员和他们演奏了多少这类电影的电影,我想找到演奏各种类型电影的演员,所以我尝试了这个:
select genre.genre_name,actor.actor_id,count(genre.genre_name)
from(select genre.genre_name,actor.actor_id,count(genre.genre_name) as max_value from genre
inner join movie_has_genre on movie_has_genre.genre_id=genre.genre_id
inner join movie on movie_has_genre.movie_id=movie.movie_id
inner join role on movie.movie_id=role.movie_id
inner join actor on actor.actor_id=role.actor_id
group by genre.genre_name,actor.actor_id
order by max_value desc) as apotelesmata
group by genre.genre_name;
我从mysql工作台得到一个错误,它不能识别我在外部选择函数上的所有内容。我的问题是我应该怎么做 得到正确的结果。 用我给出的第一个代码,我得到了这个:
Thriller 22591 7
Drama 22591 6
Crime 65536 3
Horror 22591 3
Thriller 812916 3
Action 292028 3
Action 378578 3
Thriller 292028 3
Thriller 378578 3
但我希望得到这个:
Thriller 22591 7
Drama 22591 6
Crime 65536 3
Horror 22591 3
Action 292028 3
Action 378578 3
答案 0 :(得分:1)
一个选项,相当难看:
SELECT t1.genre_name, t1.actor_id, t1.max_value
FROM
(
SELECT g.genre_name, a.actor_id, COUNT(*) AS max_value
FROM genre g
INNER JOIN movie_has_genre mhg ON mhg.genre_id = g.genre_id
INNER JOIN movie m ON mhg.movie_id = m.movie_id
INNER JOIN role r ON m.movie_id = r.movie_id
INNER JOIN actor a ON a.actor_id = r.actor_id
GROUP BY g.genre_name, a.actor_id
) t1
INNER JOIN
(
SELECT genre_name, MAX(max_value) AS max_value
FROM
(
SELECT g.genre_name, a.actor_id, COUNT(*) AS max_value
FROM genre g
INNER JOIN movie_has_genre mhg ON mhg.genre_id = g.genre_id
INNER JOIN movie m ON mhg.movie_id = m.movie_id
INNER JOIN role r ON m.movie_id = r.movie_id
INNER JOIN actor a ON a.actor_id = r.actor_id
GROUP BY g.genre_name, a.actor_id
) t
GROUP BY genre_name
) t2
ON t1.genre_name = t2.genre_name and t1.max_value = t2.max_value
ORDER BY
t1.max_value DESC;
这个答案的详细程度与您的问题的基表实际上是一个已涉及4个连接的查询有关。这个表需要在MySQL中重复,因为我们没有分析函数。此外,我们没有公用表表达式,这也可以使查询更简洁。