Home > Mobile >  MySQL Is there a query to get all null occurrences in every column through a single query?
MySQL Is there a query to get all null occurrences in every column through a single query?

Time:08-17

Currently, I check null values in MySQL by running multiple SELECT * FROM tbl <field_name> IS NULL;

This takes too much time especially if there many columns.

I wondered if there is a better way of checking all null values in one query.

CodePudding user response:

You can achieve that by :

select *
from table
where field_1 is null or field_2 is null ...;

An other way to do that is to get all columns using :

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = <table_name>

then create a procedure to loop through those columns

You can check this : Is there a quick way to check if ANY column is NULL?

CodePudding user response:

One way you can speed your query is with indexing.

SELECT * FROM tbl WHERE tbl.<field_name> IS NULL;

More info on NULL query optimization here https://dev.mysql.com/doc/refman/8.0/en/is-null-optimization.html

  • Related