Coming up with the correct title for this was a bit challenging.
So I’ve got a user profile table, a music genres table, and a genre_profiles table for the one to many association. Below I’ve got a group_concat that returns all the genreName for a profile, but there is a problem. I want to return all the genres for a profile even when I’m filtering for a specific genre like genreID 15.
SELECT *, group_concat(cast(g.genreName as char) SEPARATOR ', ') as genreName
FROM profiles p
LEFT OUTER JOIN genre_profiles gpro ON gpro.profileID = p.profileID
LEFT OUTER JOIN genres g ON g.genreID = gpro.genreID
WHERE gpro.genreID = '15'
GROUP BY p.profileID
How do I go about filtering my query for a genreID leaving the genreNames associated with a profile intact? Should I do a subquery for all the profiles with a profileID of 15 and then get all the genre data from that?
Thanks!
So I think this solves my problem.
It works in all my test cases.