If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Need help with SQL for multiple table query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-04-04, 18:15
BigE BigE is offline
Registered User
 
Join Date: Aug 2004
Posts: 1
Need help with SQL for multiple table query

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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On