I’m trying to create a query that can fetch results in a many to many relationship database.
So far I got the following:
One table with songs, one table with tags and a ‘linking’ table as one song can have multiple tags and one tag can belong to multiple songs.
it looks like this:
Songs Link Tags
======= ===== =========
Sid Sid Tid
Songname Tid Tagname
Now suppose you have 3 songs A B and C and 3 tags: X, Y and Z.
Song A has tag Y, song B has tag Z, and song C has tag X and Z.
I’ve managed to create a query to get a song by just one tag (e.g. Z gives B and C).
But how do i create a query that searches for songs when mutiple tags are enterred (e.g. typed into a (search) field).
I’ve searched and a few times the commands INTERSECT and INNER JOIN came up, but i havent been able to succesfully create a query.
Any help is appreciated!
This one will get all songs, that match any of the Tags, returning those which match all of them first:
This one will only return those songs, that match all of the Tags:
If you generate this query, you will have to generate the count too (
2in my case).