How to exclude rows that don't join with another table?

前端 未结 7 745
心在旅途
心在旅途 2020-11-30 16:48

I have two tables, one has primary key other has it as a foreign key.

I want to pull data from the primary table, only if the secondary table does not

相关标签:
7条回答
  • 2020-11-30 17:27
    SELECT
       *
    FROM
       primarytable P
    WHERE
       NOT EXISTS (SELECT * FROM secondarytable S
         WHERE
             P.PKCol = S.FKCol)
    

    Generally, (NOT) EXISTS is a better choice then (NOT) IN or (LEFT) JOIN

    0 讨论(0)
提交回复
热议问题