Brian Leishman
Brian Leishman

Reputation: 8555

MySQL Opposite of Condition

I know in other languages you can use something (usually a !) to denote the opposite of whatever follows. Is there a way to do something like this in MySQL?

For example, I have this query to select everything that isn't null or blank:

select * from `invoices` where `Notes`>'';

But is there a way way to do the opposite? Because currently I only know that you can rewrite the query such as this

select * from `invoices` where ifnull(`Notes`,'')='';

But this removes the opportunity for indexes on the Notes column or this way

select * from `invoices` where (`Notes` is null or `Notes`=''); 

But that one is a lot longer than something like

select * from `invoices` where !`Notes`>'';

which would be ideal.

Upvotes: 0

Views: 1015

Answers (1)

Mureinik
Mureinik

Reputation: 311448

SQL has a not operator:

SELECT * FROM `invoices` WHERE NOT (`Notes`>'');
-- Here -----------------------^

Upvotes: 1

Related Questions