I don't actually need the following query, but I woke up with this "theoretical problem" that I'm having trouble figuring out. Say I have three tables: a users table, groups table, and users_groups table that is a many-to-many. So if one user belongs to group 1 and 2, there would be two different rows for each.
Now, assuming that there are many groups, how do I select specifically the users that belong to both group 2 and 3, for example?
I tried something along these lines, but it showed empty:
SELECT * FROM `users_groups` GROUP BY user_id HAVING group_id = 2 AND group_id = 3
I guess that assumes that both groups are in the same row, which obviously won't work. How would I do this?
EDIT: How about both variants: where the user must ONLY be in these two groups, and the user must AT LEAST be in these two groups?