35

I have very simple query like this:

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

And my deleted_1 be default is null or some user id, but for some reason this query always returns me 0 rows, i also tried <> but still no luck what could be wrong?

EDTI So after running more querys i find out that my problems was default value of deleted_1 field, it was NULL so i modified my query and now it works fine:

SELECT *
FROM `all_conversations`
WHERE `deleted_1` != 'NULL'
AND `deleted_1` != 23
Linas
  • 4,380
  • 17
  • 69
  • 117

7 Answers7

77
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.

TimWolla
  • 31,849
  • 8
  • 63
  • 96
11

I recommend to use NULL-safe operator and negation

SELECT * FROM `all_conversations` WHERE NOT(`deleted_1` <=> '1');
Viktor Zeman
  • 489
  • 5
  • 6
4

Can you try this: deleted_1 is not null and deleted_1 != '1'?

mysql> select 0 is not null and 0 != '1', 1 is not null and 1 != '1', null is not null and null != '1';
+----------------------------+----------------------------+----------------------------------+
| 0 is not null and 0 != '1' | 1 is not null and 1 != '1' | null is not null and null != '1' |
+----------------------------+----------------------------+----------------------------------+
|                          1 |                          0 |                                0 |
+----------------------------+----------------------------+----------------------------------+

Or this deleted_1 is null or deleted_1 != '1':

mysql> select 0 is null or 0 != '1', 1 is null or 1 != '1', null is null or null != '1';
+-----------------------+-----------------------+-----------------------------+
| 0 is null or 0 != '1' | 1 is null or 1 != '1' | null is null or null != '1' |
+-----------------------+-----------------------+-----------------------------+
|                     1 |                     0 |                           1 |
+-----------------------+-----------------------+-----------------------------+

It really depends on what you wanna get back.

greut
  • 4,305
  • 1
  • 30
  • 49
2

Try This.. Hope It will work for you

SELECT *
FROM `all_conversations`
WHERE `deleted_1` IS NOT NULL
AND `deleted_1` <> 23
Kampai
  • 22,848
  • 21
  • 95
  • 95
indika
  • 21
  • 2
0

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

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0

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)
MJ X
  • 8,506
  • 12
  • 74
  • 99
-2

How about removing the single quotes around the 1?

SELECT * FROM `all_conversations` WHERE `deleted_1` != 1;
Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
  • This was my first thought as well, but http://dev.mysql.com/doc/refman/5.5/en/type-conversion.html seems to imply that this should happen automatically... – ziesemer Jan 24 '12 at 21:31
  • still nothing, at first i thought it could be problem that most of my fields are null, but even if some have a number it still doesn't select anything. – Linas Jan 24 '12 at 21:34
  • Hmm after running few more querys it finaly returned me rows where `deleted_1` is not `NULL` it's realy strange because i'm sure before it didn't return anything even if there was some numbers so i guess my problem is that field by default is `NULL` maybe i should set it to 0 then – Linas Jan 24 '12 at 21:37
  • 2
    it really depends what you want to obtain, rows that aren't `NULL` and aren't `1` or just rows that aren't `1`. – greut Jan 24 '12 at 21:42