Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Query - Not Equal to THIS and THIS

Tags:

I'm getting some weird results, so I need to just check myself...

SELECT * FROM table WHERE complete != 0 AND pending != 1 

To be clear, these are allowed:

pending = 0, complete = 0 pending = 1, complete = 1 pending = 0, complete = 1 

THis is NOT allowed to be returned from my query:

pending = 1, complete = 0 

What am I missing here...?

like image 290
Shackrock Avatar asked Nov 15 '11 00:11

Shackrock


People also ask

Can you use != In MySQL?

In MySQL, you can use the <> or != operators to test for inequality in a query. For example, we could test for inequality using the <> operator, as follows: SELECT * FROM contacts WHERE last_name <> 'Johnson';

How do you write not equal condition in MySQL?

MySQL Not Equal is an inequality operator that used for returning a set of rows after comparing two expressions that are not equal. The MySQL contains two types of Not Equal operator, which are (< >) and (! =).

Is != The same as <> in SQL?

If != and <> both are the same, which one should be used in SQL queries? Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.

What does <> mean in MySQL?

The symbol <> in MySQL is same as not equal to operator (!=). Both gives the result in boolean or tinyint(1). If the condition becomes true, then the result will be 1 otherwise 0. Case 1 − Using !=


1 Answers

Try:

SELECT * FROM table WHERE NOT (complete = 0 AND pending = 1) 

or

SELECT * FROM table WHERE !(complete = 0 AND pending = 1) 

EDIT: I went and looked at: http://dev.mysql.com/doc/refman/4.1/en/expressions.html

like image 99
David Parvin Avatar answered Oct 18 '22 14:10

David Parvin