2

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.

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

3 Comments

did you give something new or just repeat my answer ? are you a doctor in copy past things ?
@echo_Me You updated your answer by copypasting my answer. Are you a doctor in copy paste things too?
i didnt copy paste i changed =to is.
0

try this

     select * from test where age !=13  or age is NULL;

Comments

0

Use the <=> operator.

SELECT 
  * 
FROM 
  test
WHERE
  NOT( age <=> 13 );

Result:

+-------+------+
| name  | age  |
+-------+------+
| dan   |   17 |
| pete  | NULL |
| david | NULL |
+-------+------+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.