SQL query to find record with ID not in another table

后端 未结 6 1641
眼角桃花
眼角桃花 2020-12-02 09:02

I have two tables with binding primary key in database and I desire to find a disjoint set between them. For example,

  • Table1 has columns (ID
6条回答
  •  猫巷女王i
    2020-12-02 09:37

    Keeping in mind the points made in @John Woo's comment/link above, this is how I typically would handle it:

    SELECT t1.ID, t1.Name 
    FROM   Table1 t1
    WHERE  NOT EXISTS (
        SELECT TOP 1 NULL
        FROM Table2 t2
        WHERE t1.ID = t2.ID
    )
    

提交回复
热议问题