Considering 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 query
select *
from inttest
where someint != 1;
MySQL returns 2,3,2 but not the NULL value. Is this correct? Should I extend my query with OR someint IS NULL or is it a bug in my MySQL installation?
Comparison operators return
TRUE,FALSEorNULL.You’re expecting
NULL != 1to give youTRUEbut, sensibly, you getNULLfor the comparison you make. This is because comparing anything withNULLis meaningless:NULLis not a value!.Here’s a cunning trick whereby you could get the
NULLin the resultset if you still really want it. The trick relies on reversing the logic, then manually excluding theNULLpossibility:A more obvious approach might be: