SQL query question: SELECT … NOT IN

前端 未结 7 1175

I am sure making a silly mistake but I can\'t figure what:

In SQL Server 2005 I am trying select all customers except those who have made a reservation before 2 AM.<

7条回答
  •  眼角桃花
    2020-12-24 05:39

    It's always dangerous to have NULL in the IN list - it often behaves as expected for the IN but not for the NOT IN:

    IF 1 NOT IN (1, 2, 3, NULL) PRINT '1 NOT IN (1, 2, 3, NULL)'
    IF 1 NOT IN (2, 3, NULL) PRINT '1 NOT IN (2, 3, NULL)'
    IF 1 NOT IN (2, 3) PRINT '1 NOT IN (2, 3)' -- Prints
    IF 1 IN (1, 2, 3, NULL) PRINT '1 IN (1, 2, 3, NULL)' -- Prints
    IF 1 IN (2, 3, NULL) PRINT '1 IN (2, 3, NULL)'
    IF 1 IN (2, 3) PRINT '1 IN (2, 3)'
    

提交回复
热议问题