0

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?

4
  • What do you want to happen if field is null? Include in results or not? Commented Mar 22, 2014 at 12:01
  • not need to include if field contain NULL Commented Mar 22, 2014 at 12:03
  • you can just check SELECT NULL = 0; Commented Mar 22, 2014 at 12:05
  • your query already does that: A null value is not true for any comparison other than by is null and is not null Commented Mar 22, 2014 at 12:05

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Do we have some way to CAST NULL into boolean in MySQL? I tried but couldn't find.
@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.
Ok, I got it, I tried > SET @a = NULL; SELECT @a is NULL; thanks!
Thanks for this complete answer!
0

you can do this to be sure.

 SELECT table.field FROM table WHERE table.field > 0
                               AND table.field is not null

2 Comments

-1 the test for is not null adds no value, since the original test for > 0 will not be true if field is null.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.