This a follow up to the question I posed here. Using the same kind of query, how do I make sure I get 10 movies back if there are restriction like ‘genre.name = ‘Classic Movies’ and ‘star.name = Humphrey Bogart.’ To be clear, the query must return data for 10 movies regardless of what the filtering restrictions are.
SELECT movie.id, movie.title, star.name, star.name_url, dir.name,
dir.name_url, genre.name, genre.name_url
FROM
(SELECT * FROM movie WHERE movie.id > 0 ORDER BY movie.id LIMIT 10) movie
LEFT JOIN actor
ON (movie.id = actor.movie_id)
LEFT JOIN person AS star
ON (actor.person_id = star.id)
LEFT JOIN director
ON (movie.id = director.movie_id)
LEFT JOIN person AS dir
ON (director.person_id = dir.id)
LEFT JOIN genre_classification
ON (movie.id = genre_classification.movie_id)
LEFT JOIN genre
ON (genre_classification.genre_id = genre.id)
The issue is you’re selecting a list of 10 movies from the database with this sub-select:
If you’re trying to filter down that list more using a WHERE clause outside that sub-select, you could potentially get less than those 10 rows, as not all 10 could match your criteria. You could add that new criteria to the sub-select like so:
This would do the same thing, but only give you (up to) 10 movies who had a matching genre of “Classic Movies”.
This will, of course, still return more than 10 rows since you’re
JOINing in actors, directors, other genres, etc. You should, however, have up to ten distinct movie IDs in the result set.Full Query: