NULL values ​​not included in the query result

Given the following table:

create table inttest (
    someint INT(10) NULL DEFAULT NULL
);

When I insert some random values

insert into inttest 
  (someint) 
values 
  (1),(2),(3),(1),(2),(NULL);

and execute the request

select *
  from inttest 
 where someint != 1;

MySQL returns 2,3,2, but not NULL. It's right? Should I distribute my query with OR someint IS NULL or is this an error in my MySQL installation?

+3
source share
3 answers
SELECT 1 != NULL;
-- Output: NULL

Comparison operators return TRUE , FALSEor NULL.

You expect to NULL != 1give you TRUE, but it’s reasonable that you get NULLfor your comparison. This is because comparing something with is NULLmeaningless: it is not a value! . NULL

, NULL , . , NULL:

SELECT * FROM `inttest`
 WHERE IF(`someint` = 1, FALSE, TRUE);

:

SELECT * FROM `inttest`
 WHERE `someint` != '1'
    OR `someint` IS NULL;
+1

. NULL - NULL. , NULL != 1 UNKNOWN - WHERE TRUE.

+3

NULL UNKNOWN.

someint <> 1 SQL , 1. NULL UNKNOWN, 1, . - SQL , , 1.

0

All Articles