[SOLVED] Test for NULLs in multiple columns in MySQL

Issue

I want to select columns from a table where the values of columns are NULL.

For example:

SELECT * FROM table1 WHERE column1 IS NULL;

the above query returns rows where column1 doesn’t have any value.

But I want to return multiple columns(where column1,column2,….)

I want to use this in MYSQL

Solution

SELECT *
FROM table1
WHERE coalesce(column1, column2, column3) IS NULL;

You will have to enumerate all required columns. (I should confess this is a hack and should not be used in production code)

UPD

IF you want to check if at least a single column is null you should use OR:

SELECT *
FROM table1
WHERE column1 IS NULL or column2 IS NULL;

Answered By – newtover

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.