table 1
item group_id
123 aaaa
432 bbbb
534 aaaa
765 cccc
656 aaaa
656 cccc
111 cccc
group_id group_name
aaaa groupA
bbbb groupB
cccc groupC
I want to find all groups that contain both 765 and 656 - in this instance GroupC contains both of those values.
Is there a way that avoids a bunch of nested queries or AND statements? Say if table 1 can have thousands of pairings and I have to find the groups that contain all of a list of 30 items It would be ugly to write a really big query to answer the question.
I wish I could say "where in (765,656)" but have it behave differently. If there was a bunch of items to match on it would get messy.
SQL Fiddle http://sqlfiddle.com/#!9/6581eb/9
Sample data (in case SQL Fiddle is inaccessible for some reason):
create table table1(itemID int,groupID varchar(4));
insert into table1(itemID,groupID)
values
(123,'aaaa')
,(432,'bbbb')
,(534,'aaaa')
,(765,'cccc')
,(656,'aaaa')
,(656,'cccc')
,(111,'cccc');
create table table2(groupID varchar(4),groupName varchar(6));
insert into table2(groupID,groupName)
values
('aaaa','groupA')
,('bbbb','groupB')
,('cccc','groupC');