SQL Server - NOT IN

前端 未结 6 867
情书的邮戳
情书的邮戳 2020-12-29 19:50

I need to build a query that will show me records that are in Table 1, but that are not in Table 2, based on the make-model-serial number combination.

I know for fac

6条回答
  •  难免孤独
    2020-12-29 20:31

    It's because of the way NOT IN works.

    To avoid these headaches (and for a faster query in many cases), I always prefer NOT EXISTS:

    SELECT  *  
    FROM Table1 t1 
    WHERE NOT EXISTS (
        SELECT * 
        FROM Table2 t2 
        WHERE t1.MAKE = t2.MAKE
        AND   t1.MODEL = t2.MODEL
        AND   t1.[Serial Number] = t2.[serial number]);
    

提交回复
热议问题