query “not equal” doesn't work

前端 未结 7 2091
时光说笑
时光说笑 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:44
    SELECT * FROM all_conversations WHERE deleted_1 <> 1 OR deleted_1 IS NULL
    

    NULL values need special treatment: http://dev.mysql.com/doc/refman/5.1/en/working-with-null.html

    I'd suggest using the diamond operator (<>) in favor of != as the first one is valid SQL and the second one is a MySQL addition.

    0 讨论(0)
  • 2020-12-05 23:47

    I agree with above Answer, just add extra detail if you have multiple AND condition like below query, you should put the column you check for not equals with or to null in first part of the Where condition, because the OR part makes your condition incorrect

      SELECT ts.*, ct.Name AS CategoryName
    FROM MJ.Tasks  AS ts 
    LEFT JOIN MJ.Lookup_Category AS ct ON ts.CategoryID = ct.ID
    WHERE ts.Status!=1 OR ts.Status IS NULL AND ts.CategoryID = @CategoryID AND ts.UserID = @UserID AND  CAST(ts.EndDate AS DATE) = CAST(@EndDate AS DATE)
    
    0 讨论(0)
  • 2020-12-05 23:49

    Try This.. Hope It will work for you

    SELECT *
    FROM `all_conversations`
    WHERE `deleted_1` IS NOT NULL
    AND `deleted_1` <> 23
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-05 23:57

    How about removing the single quotes around the 1?

    SELECT * FROM `all_conversations` WHERE `deleted_1` != 1;
    
    0 讨论(0)
  • 2020-12-06 00:01

    I recommend to use NULL-safe operator and negation

    SELECT * FROM `all_conversations` WHERE NOT(`deleted_1` <=> '1');
    
    0 讨论(0)
提交回复
热议问题