Say I have the following 3 tables:
users
id : int 11 primary autoinc
email : varchar 255
tags
id : int 11 primary autoinc
name : varchar 255
tag_union
id : int 11 primary autoinc
tag_id : int 11
target_id : int 11
target_type : enum( 'user','blog','other' )
If I'm given a list of tags, say 1,3, and 8, I can select all of the users that have a union of ANY of the tags like so:
SELECT *
FROM `users`
WHERE `id` IN( SELECT `target_id`
FROM `tag_union`
WHERE `tag_id` IN( '1','3','8')
&& `target_type`=='user' )
But how would I select only users that have a union for ALL 3 of the tags? I can't seem to think of a way to do this in a single query.
P.S. Sorry for the crappy title, someone can rename it if they can think of a more fitting one.
INwill take several seconds or more, whereas the JOIN will be almost instant. But if I'm wrong about that, I'd like to know so that I can learn.