EchoFool Posted November 30, 2009 Share Posted November 30, 2009 Hey - i was curious what MYSQL function i should use to get ids from one table which is not found in 2 other tables? I tried this: SELECT RecordID FROM states WHERE RecordID NOT IN (SELECT RecordID FROM trade WHERE (StateID1='$Info[12]' OR StateID2='$Info[12]') AND (StateID1=states.RecordID OR StateID2=states.RecordID)) AND RecordID NOT IN (SELECT RecordID FROM businesses WHERE (StateID1='$Info[12]' OR StateID2='$Info[12]') AND (StateID1=states.RecordID OR StateID2=states.RecordID)) How ever this is giving me all ids which are found in at least one of the tables from the "NOT IN" part. How do i make a query to fine ids not found in the other two tables ? Quote Link to comment https://forums.phpfreaks.com/topic/183382-select-id-which-is-not-found-in-2-other-tables/ Share on other sites More sharing options...
kickstart Posted November 30, 2009 Share Posted November 30, 2009 Hi Try something like this. SELECT a.RecordID FROM states a LEFT OUTER JOIN (SELECT RecordID FROM trade WHERE (StateID1='$Info[12]' OR StateID2='$Info[12]')) b ON a.RecordID = b.RecordID LEFT OUTER JOIN (SELECT RecordID FROM businesses WHERE (StateID1='$Info[12]' OR StateID2='$Info[12]')) c ON a.RecordID = c.RecordID WHERE b.RecordID IS NULL AND c.RecordID IS NULL All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/183382-select-id-which-is-not-found-in-2-other-tables/#findComment-968033 Share on other sites More sharing options...
EchoFool Posted November 30, 2009 Author Share Posted November 30, 2009 Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/183382-select-id-which-is-not-found-in-2-other-tables/#findComment-968351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.