简体   繁体   中英

Get rows data at least one of null in columns MySQL

I had a table look like this(There will actually be more columns in my table).

CREATE TABLE mytable(
  ID int PRIMARY KEY,
  Name varchar(50),
  teamName varchar(255),
  Create_Date DATETIME
);

Here is some sample data

INSERT INTO mytable VALUES (1,NULL,'TEST1','2017-01-02');
INSERT INTO mytable VALUES (2,NULL,'TEST2',NULL);
INSERT INTO mytable VALUES (3,'KK','TEST3','2017-01-02');
INSERT INTO mytable VALUES (4,NULL,NULL,NULL);

I want to get rows,which at least one null in the columns.

I had tried this. I got the expect result.

SELECT * 
FROM mytable
WHERE  Name IS NULL OR teamName IS NULL OR Create_Date IS NULL

Expect result

| ID |   Name | teamName |          Create_Date |
|----|--------|----------|----------------------|
|  1 | (null) |    TEST1 | 2017-01-02T00:00:00Z |
|  2 | (null) |    TEST2 |               (null) |
|  4 | (null) |   (null) |               (null) |

But I don't know if there is an easier way to achieve this effect.

I tried to use IN with IS NULL, but it didn't work.

SELECT * 
FROM mytable
WHERE IS NULL IN (Name,Create_Date,teamName)

Here is sample data http://sqlfiddle.com/#!9/3b678d/1

Just directly check using IS NULL if any of the three columns be NULL :

SELECT *
FROM mytable
WHERE Name IS NULL OR Create_Date IS NULL OR teamName IS NULL;

I think you can't use IS NULL IN

NULL mean I don't know

Working with NULL Values

the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons.

you can use + to add all columns if there is a column is null in the columns the result will be null .

SELECT * 
FROM mytable
WHERE (Name+teamName+Create_Date) IS NULL

there is way of using IN but your way is better than this

select * from mytable where id in
(SELECT id
FROM mytable
WHERE  Name IS NULL union
SELECT id
FROM mytable
WHERE  teamName IS NULL union
SELECT id
FROM mytable
WHERE  Create_Date IS NULL
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM