Reputation: 1
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
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
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