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 > Join question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-12-11, 14:11
Scotty2024 Scotty2024 is offline
Registered User
 
Join Date: Nov 2008
Posts: 8
Question Join question

Hello. I'm trying to create a query to join on two tables.

Table1 has id and name. Table2 has userID and itemID.

Table1 id and table2 userID are the same. For a specific itemID I want to select all names from table1 where the userID in table2 does not exists.

For example, here is some test data.

Table1
1 Bill
2 Bob
3 Joe

Table2
1 1
2 1
3 2

If I wanted to get all names where the userID wasn't listed for itemID 1, it would return Joe. Similarly if I wanted to get all names where the userID wasn't listed for itemID 2, it would return Bob and Joe. Finally, if I wanted to get all names where the itemID was not equal to 1 or 2, it would return Bill, Bob, and Joe.

How do I join the two tables to do this? Thanks.
Reply With Quote
  #2 (permalink)  
Old 03-12-11, 21:32
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by Scotty2024 View Post
For a specific itemID I want to select all names from table1 where the userID in table2 does not exists.
presumably you mean "where the userID in table2 does not exists in Table1 as id", correct?
Code:
SELECT Table2.userid
  FROM Table2
LEFT OUTER
  JOIN Table1
    ON Table1.id = Table2.userid
 WHERE Table2.itemID = 937
   AND Table1.id IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-12-11, 22:27
Scotty2024 Scotty2024 is offline
Registered User
 
Join Date: Nov 2008
Posts: 8
Hi r937.

I think the query you provided is close. Except I am looking to get the names from Table1 where a the user is not connected to a given itemID in Table2.

Using the example data in my first post, if I wanted to see what names weren't attached to Table2.itemID = 1 it would return Joe. Or if I wanted to see which names weren't attached to Table2.itemID = 2 it would return Bob and Joe. Last, if I wanted to see which names weren't attach to anything other than Table2.itemID != 1 OR Table2.itemID != 2 then it would return all names.

Does this make sense?
Reply With Quote
  #4 (permalink)  
Old 03-12-11, 22:47
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by Scotty2024 View Post
Except I am looking to get the names from Table1 where a the user is not connected to a given itemID in Table2.
so, basically, the other way around
Code:
SELECT Table1.name
  FROM Table1
LEFT OUTER
  JOIN Table2
    ON Table2.userid = Table1.id
   AND Table2.itemID = 937
 WHERE Table2.userid IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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