I need to return values that appear across two or more queries from the same table. Feeling a bit like a noob here but just can’t see the wood for the trees.
I have a table of users assigned to different groups. I need to return only the users that appear in all of the requested groups:
User Group
frank 1
Jane 2
Tom 2
frank 2
Jane 1
Tom 3
This is want I need to output:
Result for Group 1 & 2:
frank
Jane
Result for Group 2 & 3:
Jane
Tom
Seems a very 101 question, but grateful for any pointers.
You need to adjust the
having count(*) = 2condition to the number of groups that you want.If a user can be assigned to the same group more than once, then Madhivanan’s comment need to be taken into account:
(I used the column name
group_idbecausegroupis a reserved word and should be used as a column name).