I use query like this:
SELECT table.field FROM table WHERE table.field > 0
Would WHERE clause be working if table.field contain NULL value?
2 Answers
The where clause will work the way it is supposed to. Just about all comparisons to NULL return NULL, which is treated as "false" in the where clause.
So, all these expressions will return no rows if the value is NULL:
WHERE table.field > 0
WHERE table.field = 0
WHERE table.field < 0
WHERE table.field <> 0
If you are concerned about NULL values, test for them explicitly (using is null or is not null) or use a function like coalesce() to give the field a value when it is NULL.
4 Comments
Grijesh Chauhan
Do we have some way to CAST NULL into boolean in MySQL? I tried but couldn't find.
Gordon Linoff
@GrijeshChauhan . . . You don't need to cast it. Just say
field is not null and MySQL gives a boolean result, which is "true" (1) if the field is not null and "false" (0) if the field is.Grijesh Chauhan
Ok, I got it, I tried
> SET @a = NULL; SELECT @a is NULL; thanks!abdulmanov.ilmir
Thanks for this complete answer!
you can do this to be sure.
SELECT table.field FROM table WHERE table.field > 0
AND table.field is not null
2 Comments
Bohemian
-1 the test for
is not null adds no value, since the original test for > 0 will not be true if field is null.Bohemian
but now it will return every non-null row, eg if
field is -1 it will be returned: That hardly makes sense if the original test was for field > 0. You are barking up the wrong tree.
fieldis null? Include in results or not?fieldcontainNULLSELECT NULL = 0;is nullandis not null