retrieve NOT related data many to many relation

25 pts.
Tags:
SQL
I have 3 tables. one of the tables is a linked table, so it contains the primary key from table 1 and primary key from table 2 as foreign keys. together they make up the primary key for the linked table. Now i need to find out which items are not in the linked table. example : table A contains camping facilities, table B contains availability periods, table C is the cominbination of the chosen camping facility with a specific period. Now i need to know which facilities and periods are still available (all combination not in table C)
ASKED: January 21, 2008  11:01 AM
UPDATED: January 24, 2008  5:18 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

One way to find all the rows in table a that do not have a match in table c is to use a NOT IN subquery.

<pre>
SELECT …
FROM a
WHERE facility_id NOT IN
( — Begin sub-query of all facility_ids in table c
SELECT facility_id
FROM c
WHERE facility_id IS NOT NULL
) — End sub-query of all facility_ids in table c
;
</pre>

Do another, similar query for table b.

NOT IN is a short-cut for doing not-equals operations: “facility_id NOT IN (1, 2)” is equivalent to “facility_id <> 1 AND facility_id <> 2″, and “facility_id NOT IN (subquery)” is equivalent to asserting that facility_id is not equal to any row returned by the sub-query. Note that if any row returned by the sub-query is NULL, then SQL can’t make that assertion, so remember check for NULLs in the sub-query.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • GregMM
    a simple outer join will show you the unmatched rows as they will have nulls in the "common" columns
    60 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following