我在MySQL 8.0中有这个丑陋的代码。
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Adventure'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Music'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Fantasy'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Romance'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Family'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Comedy'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Short'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Animation'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Action'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Horror'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Sci-Fi'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Thriller'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Crime'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Drama'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Documentary'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Musical'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Mystery'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='War'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Western'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Adult'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3)
UNION
(SELECT genre_name, actor_id, count(actor_id)
FROM movie_has_genre, role, movie, genre
where role.movie_id=movie.movie_id
and movie_has_genre.movie_id=movie.movie_id
and genre.genre_id=movie_has_genre.genre_id
and genre_name='Film-Noir'
group by genre_name, actor_id
ORDER BY count(actor_id) DESC
limit 3);
我希望将其转换为更简洁。基本上,唯一改变的是genre_name条件。可以找到此条件的21个值:select genre_name from genre;
如何遍历genre_name的21个值并将它们插入到select查询中,逐步构建UNION查询?感谢。
答案 0 :(得分:0)
您可以使用ROW_NUMBER
:
WITH cte AS (
SELECT genre_name, actor_id, count(actor_id) AS cnt
,ROW_NUMBER() OVER(PARTITION BY genre_name ORDER BY count(actor_id) DESC) rn
FROM movie
JOIN movie_has_genre ON movie_has_genre.movie_id=movie.movie_id
JOIN role ON role.movie_id=movie.movie_id
JOIN genre ON genre.genre_id=movie_has_genre.genre_id
group by genre_name, actor_id
)
SELECT *
FROM cte
WHERE rn <=3;
请不要使用过时的逗号连接语法。
工作原理:
答案 1 :(得分:0)
在MySQL(8.0之前)中,您可以使用变量:
select genre_name, actor_id, cnt
from (select genre_name, actor_id, cnt,
(@rn := if(@g = genre_name, @rn + 1,
if(@g := genre_name, 1, 1)
)
) as rn
from (select g.genre_name, r.actor_id, count(*) as cnt
from movie m join
role r
on r.movie_id = m.movie_id join
movie_has_genre mhg
on mhg.movie_id = m.movie_id join
genre g
on g.genre_id = mhg.genre_id
group by g.genre_name, r.actor_id
order by g.genre_name, cnt desc
) ga cross join
(select @g := '', @rn := 0) params
) ga
where rn <= 3;
在8.0+中,使用row_number()
ANSI标准方法来实现此功能。
注意:
FROM
子句中使用逗号。 始终使用正确的JOIN
语法。FROM
子句引用多个表时。