Select rows that have at least one null value [duplicate]
Clash Royale CLAN TAG#URR8PPP
Select rows that have at least one null value [duplicate]
This question already has an answer here:
I had a database that allowed NULL
in many of its columns (in all tables). I would like to know if there is a clean way to select from a table all the rows that have at least one value set to null in any column.
NULL
I know that this can be achieved by using:
SELECT * FROM `my_table` WHERE `column_1` is null OR `column_2` is null ... with all columns
but I want to know if there is a cleaner and more general way so I must not make a new query for every new table.
Thanks.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
information_schema.COLUMNS
CONCAT
SELECT
2 Answers
2
Sorry But it cant be done a cleaner way
If you could select all the rows and then apply a function it could be made but it's just not the way SQL was built.
What you "know" is wrong. The correct logic would be:
where column_1 is null or column_2 is null or . . .
This is the correct syntax and a perfectly reasonable approach.
thanks for the correction, that's what i meant. I corrected my post. Though I want to know if there is a more general way that i can apply to many different tables with different column names
– Dknacht
Aug 10 at 19:36
@Dknacht There's nothing that automatically loops over columns dynamically. You could write a stored procedure that gets the column names from
INFORMATION_SCHEMA
and builds the SQL dynamically.– Barmar
Aug 10 at 19:52
INFORMATION_SCHEMA
You can use
information_schema.COLUMNS
to generate (viaCONCAT
)SELECT
statement(s) with all the columns for all tables.– Rick James
Aug 26 at 0:41