0

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135

3 Answers3

2

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

WORKING WITH NULL VALUES IN MYSQL

1000111
  • 13,169
  • 2
  • 28
  • 37
1

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

Community
  • 1
  • 1
1

Need to check for NULL condition separately..

and column is NULL
Harika B
  • 83
  • 9