I have very simple query like this:
SELECT * FROM `all_conversations` WHERE `deleted_1` != \'1\';
And my deleted_1
be default
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.
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)
Try This.. Hope It will work for you
SELECT *
FROM `all_conversations`
WHERE `deleted_1` IS NOT NULL
AND `deleted_1` <> 23
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
How about removing the single quotes around the 1?
SELECT * FROM `all_conversations` WHERE `deleted_1` != 1;
I recommend to use NULL-safe operator and negation
SELECT * FROM `all_conversations` WHERE NOT(`deleted_1` <=> '1');