Scotty2024 Posted March 12, 2011 Share Posted March 12, 2011 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. Link to comment https://forums.phpfreaks.com/topic/230463-join-question/ Share on other sites More sharing options...
fenway Posted March 13, 2011 Share Posted March 13, 2011 LEFT JOIN... IS NULL. Link to comment https://forums.phpfreaks.com/topic/230463-join-question/#findComment-1186835 Share on other sites More sharing options...
Scotty2024 Posted March 13, 2011 Author Share Posted March 13, 2011 I'm not strong with left/right join. So would it be something like this: select name from table1 left join on table2 on table1.id = table2.userID Link to comment https://forums.phpfreaks.com/topic/230463-join-question/#findComment-1186845 Share on other sites More sharing options...
fenway Posted March 13, 2011 Share Posted March 13, 2011 Almost -- add "where table2.userID is null". Link to comment https://forums.phpfreaks.com/topic/230463-join-question/#findComment-1187008 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.