Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for NULLs in multiple columns in MySQL

Tags:

mysql

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

like image 691
Santosh Avatar asked Dec 27 '11 12:12

Santosh


People also ask

How do you find which columns have NULL values in MySQL?

To search for column values that are NULL , you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression: mysql> SELECT * FROM my_table WHERE phone = NULL; To look for NULL values, you must use the IS NULL test.


1 Answers

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;
like image 124
newtover Avatar answered Sep 26 '22 13:09

newtover