Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2012
    Posts
    2

    Question Unanswered: Exclude records from query results of tableA when records exist in tableB

    I am trying to write a query where I exclude records from my query results from TableA when records with two key fields exist in tableB.

    I can get it to exclude from the return results when I am trying to exclude based on just one keyfield but I can't get it to work when I want to use two fields as the exclusion criterion.

    Here is what I am using so far:
    SELECT *
    FROM TableA
    WHERE (((TableA.PK1) Not In (select PK1 from TableB))

    I have tried just adding a second WHERE or an AND but don't seem to be getting anywhere...except maybe greyer!

    Please help!
    thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT TableA.*
      FROM TableA
    LEFT OUTER
      JOIN TableB
        ON TableB.pk1 = TableA.pk1
       AND TableB.pk2 = TableA.pk2
     WHERE TableB.pk1 IS NULL
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Aug 2012
    Posts
    2

    That did it

    Awesome! That did it! Thanks for the help. I had been scratching my head all day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •