TGsera
TGsera

Reputation: 1

MYSQL Query that excludes falsy values

Is there an easy way in MYSQL to exclude all falsy values in one statement? It should do the following:

SELECT id FROM table 
WHERE id != '' 
AND id != 0 
AND id IS NOT NULL;

Thanks, Tom

Upvotes: 0

Views: 572

Answers (2)

Carlos M Gomez
Carlos M Gomez

Reputation: 85

I know that it happens a lot of time, but I have another proposal:

SELECT * FROM table WHERE COALESCE(NULLIF(field,0)); 

I hope this could be helpful to someone!

Upvotes: 0

Simo Kivistö
Simo Kivistö

Reputation: 4473

If we presume your column is varchar, then you can use COALESCE to turn NULL into one of those invalid values:

SELECT id FROM table 
 WHERE COALESCE(id, '') NOT IN ('', '0');

Upvotes: 1

Related Questions