Making a query to sort albums by genre problem
So I have 3 tables, "music", "genres", and "music_genres", music being a table of albums, genres being a table of genres, and music_genres being a table of links bet开发者_StackOverflow中文版ween the two tables (each row has a link to music and genres).
Now I'm trying to take an input set of genres that I want to filter my results by when I select all of the music. I basically want to say if the genre_id is IN() a predefined array that I have of id's, but only show that entry from the music table if it has one entry from the music_genres table linking it to one of the specified genre_ids..
Any thoughts?
SELECT
music.*
FROM
music
INNER JOIN
music_genres
ON
music.id = music_genres.music_id
WHERE
music_genres.genre_id IN (1,2,3)
Use SELECT DISTINCT
or a GROUP BY
clause if you don't want duplicate results for music in multiple genres.
精彩评论