query “not equal” doesn't work

前端 未结 7 2106
时光说笑
时光说笑 2020-12-05 23:01

I have very simple query like this:

SELECT * FROM `all_conversations` WHERE `deleted_1` != \'1\';

And my deleted_1 be default

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 23:53

    You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL.

    To demonstrate this for yourself, try the following query:

    mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
    +----------+-----------+----------+----------+
    | 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
    +----------+-----------+----------+----------+
    |     NULL |      NULL |     NULL |     NULL |
    +----------+-----------+----------+----------+
    

    so you have to say,

    SELECT * FROM `all_conversations` WHERE `deleted_1` <> '1' and `deleted_1` is null;
    

    your_field IS NULL explicitly since NULL cant be club with values using arithmetic operators and it has own value BOOLEAN

提交回复
热议问题