mysql: select query when the column that has condition on has NULL value

99封情书 提交于 2019-12-24 18:12:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!