Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query "not equal" doesn't work

Tags:

sql

mysql

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 
like image 844
Linas Avatar asked Jan 24 '12 21:01

Linas


People also ask

Is != And <> the same in SQL?

Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.

How do you use not equal to in query?

The SQL Not Equal comparison operator (!=) is used to compare two expressions. For example, 15 != 17 comparison operation uses SQL Not Equal operator (!=) between two expressions 15 and 17.

What does != Mean in query?

<> means NOT EQUAL TO, != also means NOT EQUAL TO.

Is != Valid in SQL?

There is no != operator according to the ANSI/SQL 92 standard.


2 Answers

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.

like image 133
TimWolla Avatar answered Oct 12 '22 10:10

TimWolla


I recommend to use NULL-safe operator and negation

SELECT * FROM `all_conversations` WHERE NOT(`deleted_1` <=> '1'); 
like image 37
Viktor Zeman Avatar answered Oct 12 '22 10:10

Viktor Zeman