对某个列的组最大值有问题

nnt7mjpx  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(247)

我想找到每一种类型的电影,找到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 workbench得到一个错误,它不能识别我在外部select函数上的几乎所有内容,我的问题是我应该怎么做才能得到正确的结果。根据我给出的第一个代码,我得到:

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
4xrmg8kj

4xrmg8kj1#

一个选择,相当难看:

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中重复,因为我们没有分析函数。而且,我们没有公共表表达式,这也可以使查询更加简洁。

相关问题