SQL WHERE condition is not equal to?

前端 未结 10 1840
一生所求
一生所求 2020-12-07 21:48

Is it possible to negate a where clause?

e.g.

DELETE * FROM table WHERE id != 2;
相关标签:
10条回答
  • 2020-12-07 22:21

    I was just solving this problem. If you use <> or is not in on a variable, that is null, it will result in false. So instead of <> 1, you must check it like this:

     AND (isdelete is NULL or isdelete = 0)
    
    0 讨论(0)
  • 2020-12-07 22:30

    Yes. If memory serves me, that should work. Our you could use:

    DELETE FROM table WHERE id <> 2
    
    0 讨论(0)
  • 2020-12-07 22:31

    You can do like this

    DELETE FROM table WHERE id NOT IN ( 2 )
    

    OR

    DELETE FROM table WHERE id <>  2 
    

    As @Frank Schmitt noted, you might want to be careful about the NULL values too. If you want to delete everything which is not 2(including the NULLs) then add OR id IS NULL to the WHERE clause.

    0 讨论(0)
  • 2020-12-07 22:31

    Use <> to negate the where clause.

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