When I write column!=something it also doesn't choose any null value and something. Why this happened? Can I prevent this just writing AND column is NULL?
3 Answers
You need to check for NULL too.
SELECT
*
FROM your_table
WHERE column <> something
OR column IS NULL
Note: You cannot compare NULL with anything but NULL.
Let's look at the following code snippets:
SET @v := NULL;
SELECT @v = NULL;
Result: NULL; Because NULL can be only compared using IS NULL
SELECT @v IS NULL;
Result: 1 (i.e. TRUE)
The NULL value can be surprising until you get used to it. Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values.
To test for NULL, use the IS NULL and IS NOT NULL
1 Comment
Yes, you have check for NULL using the OR operator as 1000111 specified, not AND.
Also as you already know when using OR, if there are other AND operators in the WHERE clause then we need to enclose them with brackets ()
Regards
Comments
Need to check for NULL condition separately..
and column is NULL
nullwill allways be not fullfilling the condition if it is not beeing checked explicity.