I have this ugly code here in 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);
and I want to convert it to be more concise. Basically, the only thing that changes is the genre_name condition. The 21 values for this condition can be found running: select genre_name from genre;
How can I loop through the 21 values of genre_name and plug them into the select query, incrementally building up the UNION query? Thanks.
See Question&Answers more detail:
os