Please help with the SQL statement to achieve the following:
Two tables:
Table 1:
GROUP
ID
Table 2:
ID
NAME
DESCRIPTION
The same ID can belong to many GROUP's.
Some ID's may not belong to any groups.
Here is a set of sample values:
Table1:
GROUP ID
'GROUP1' 11
'GROUP1' 14
'GROUP1' 15
'GROUP2' 10
'GROUP2' 11
'GROUP3' 10
'GROUP3' 12
'GROUP3' 14
'GROUP3' 15
Table2:
ID NAME DESCRIPTION
10 'WIDGET' 'AAAAAAAAAA'
11 'THINGY' 'BBBBBBBBBB'
12 'WHATSIT' 'CCCCCCCCCC'
13 'WHOSIT' 'DDDDDDDDDD'
14 'WATCHIT' 'EEEEEEEEEE'
15 'THANG' 'FFFFFFFFFF'
Given the GROUP, I want to display all items from Table 2 that do not belong to that GROUP. This includes items belonging to other GROUPS and items not belonging to any GROUPS.
Example:
GROUP='TEAM1'
Result should be:
10 'WIDGET' 'AAAAAAAAAA'
12 'WHATSIT' 'CCCCCCCCCC'
13 'WHOSIT' 'DDDDDDDDDD'
10 and 12 belong to other GROUPS, but not this one, and 13 belongs to no GROUPS.
GROUP='TEAM2'
Result should be:
12 'WHATSIT' 'CCCCCCCCCC'
13 'WHOSIT' 'DDDDDDDDDD'
14 'WATCHIT' 'EEEEEEEEEE'
15 'THANG' 'FFFFFFFFFF'
12, 14 and 15 belong to other GROUPS, but not this one, and 13 belongs to no GROUPS.
GROUP='TEAM3'
11 'THINGY' 'BBBBBBBBBB'
13 'WHOSIT' 'DDDDDDDDDD'
11 belongs to other GROUPS, but not this one, and 13 belongs to no GROUPS.
I can get all items not belonging to any groups by using:
SELECT * FROM TABLE2 LEFT JOIN TABLE1 USING (ID) WHERE TABLE1.ID IS NULL
I can't figure out the second part where I want to also include items that belong to other GROUPS, but not the current one.
Please help.
Thanks,
E