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

前端 未结 7 743
心在旅途
心在旅途 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:02

    use a "not exists" left join:

    SELECT p.*
    FROM primary_table p LEFT JOIN second s ON p.ID = s.ID
    WHERE s.ID IS NULL
    

提交回复
热议问题