问题
This is my example table:
CREATE TABLE test(
name VARCHAR(35),
age INT(3))
And some values:
insert into test values ('alex', 13);
insert into test values ('dan', 17);
insert into test (name) values ('pete');
insert into test (name) values ('david');
When I use SELECT query with the condition on column 'age':
select * from test where age!=13;
The result I got is:
+------+------+
| name | age |
+------+------+
| dan | 17 |
+------+------+
But I want all the record with age!=13 which includes the record with age IS NULL too:
+-------+------+
| name | age |
+-------+------+
| dan | 17 |
| pete | NULL |
| david | NULL |
+-------+------+
How can I get what I wants? Thanks for any responding.
回答1:
SELECT * FROM test WHERE age!=13 OR age IS NULL;
Keep in mind that NULL is not really a value but more like a state. NULL = NULL
is always false as NULL != NULL
. Whatever you use with NULL
will always be false. So when it evaluates age != 13
to NULL != 13
, it is the expected behavior to not show the row.
回答2:
try this
select * from test where age !=13 or age is NULL;
来源:https://stackoverflow.com/questions/18748862/mysql-select-query-when-the-column-that-has-condition-on-has-null-value