Jump to content

Select id which is not found in 2 other tables?


EchoFool

Recommended Posts

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 ?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.