I think I found the answer to my question, I\'m just unsure of the syntax, I keep getting SQL errors.
Basically, I want to do the opposite of IN. Take this example:<
SELECT Users.id, count(tags.*) as tagCount
FROM Users Inner join tags
ON tags.user_id = users.id
WHERE tags.name='tag1' OR tags.name='tag2'
GROUP BY Users.id
HAVING count(tags.*) = 2
Give this a try
SELECT *
FROM users
INNER JOIN tags ON tags.user_id = users.id
WHERE users.id in
(
SELECT user_id
FROM tags
WHERE name IN ('tag1', 'tag2')
GROUP BY user_id
HAVING COUNT(*) = 2
)
select * from users u
where 2 = (select count(*) from tags t where t.user_id = u.id and name in ('tag1','tag2'))
Assuming that any given tag can only be present once per user.
You'll want to join the tags table again.
SELECT * FROM users
INNER JOIN tags as t1 on t1.user_id = users.id and t1.name='tag1'
INNER JOIN tags as t2 on t2.user_id = users.id and t2.name='tag2'
You'll need to check for the existence of two rows, rather than being able to do a simple IN
(which will only check the values within each joined record). Maybe something like:
SELECT *
from users
WHERE EXISTS (SELECT NULL FROM tags WHERE tags.user_id = users.id AND tags.name = 'tag1')
AND EXISTS (SELECT NULL FROM tags WHERE tags.user_id = users.id AND tags.name = 'tag2');