mysql:条件为条件的列的值为NULL时,选择查询 [英] mysql: select query when the column that has condition on has NULL value
问题描述
这是我的示例表:
CREATE TABLE test(
name VARCHAR(35),
age INT(3))
还有一些值:
insert into test values ('alex', 13);
insert into test values ('dan', 17);
insert into test (name) values ('pete');
insert into test (name) values ('david');
当我在条件为年龄的情况下使用SELECT查询时:
When I use SELECT query with the condition on column 'age':
select * from test where age!=13;
我得到的结果是:
+------+------+
| name | age |
+------+------+
| dan | 17 |
+------+------+
但是我想要所有具有年龄的记录!= 13,其中也包括具有年龄IS NULL的记录:
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.
推荐答案
SELECT * FROM test WHERE age!=13 OR age IS NULL;
请记住,NULL并不是真正的值,而更像是一个状态。 NULL = NULL
始终为false,因为 NULL!= NULL
。与 NULL
一起使用时,始终为false。因此,当它将 age!= 13
评估为 NULL!= 13
时,不显示该行是预期的行为
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.
这篇关于mysql:条件为条件的列的值为NULL时,选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!