i want help how to solve this sql problem.
suppose i have 3 tables
Movie
- ID
- Name
Genre
- ID
- Name
Movie_Genre (this one is the link for many to many)
- FK_MovieID
- FK_GenreID
i want to select all the movies that are of genre 1 and genre 3
how is this possible?
i can only select the movies of 1 genre but not the movies that are of 2 genres using
SELECT Movie.ID, Movie.Name
FROM Movies
INNER JOIN Movie_Genre ON Movie_Genre.FK_MovieID=Movie.ID
AND Movie_Genre.FK_GenreID = 1
Use:
The last line is key to getting rows from both genre’s – the DISTINCT means duplicate associations (IE: two instances of genre 1) will be ignored because they are false positives. But the count must equal the number of genres you are looking for.
But
COUNT(DISINCTisn’t supported by all databases. You should mention what you are using – if not by tag, then in the question… If the primary key for theMOVIE_GENREtable is bothfk_movieidandfk_genreid, then it’s not an issue. Next best thing would be that both the columns are in a unique constraint/index…