Skip to content
Advertisement

How to query many-to-many relation with features table (AND condition)

I guess this is a common setting, but as I don’t do that much SQL work, I can’t get my head around this one… So, I’ve got a bunch of songs that have certain features (style of music, mood etc.) and I would like to select songs that are attributed some of these features (e. g. songs that are happy and euphoric).

I would like to select all the songs that have certain features with an AND condition. I would use this query for the OR-case.

But this obviously does not work for the AND condition, so I guess I’m on the wrong track here… Any hints will be greatly appreciated.

Advertisement

Answer

You can do it with aggregation, if you filter the resultset of the joins and set the condition in the HAVING clause:

See the demo.
Results:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement