如何使用PostgreSQL在任何列中查找具有NULL值的所有行

ico*_*ast 23 sql postgresql

有许多略有相似的问题,但没有一个能解决这个问题." 在任何列中查找具有空值的所有行 "是我能找到的最接近的一个并为SQL Server提供答案,但我正在寻找一种在PostgreSQL中执行此操作的方法.

如何只选择任何列中具有NULL值的行?

我可以轻松地获得所有列名称:

select column_name from information_schema.columns where table_name = 'A';
Run Code Online (Sandbox Code Playgroud)

但目前还不清楚如何检查多个列名称的NULL值.显然这不起作用:

select* from A where (
  select column_name from information_schema.columns where table_name = 'A';
) IS NULL;
Run Code Online (Sandbox Code Playgroud)

并且搜索没有发现任何有用的东西.

Mar*_*rth 37

你可以用NOT(<table> IS NOT NULL).

文档:

如果表达式是行值,则当行表达式本身为null或所有行的字段为空时,IS NULL为true,而当行表达式本身为非null且所有行的字段为空时,IS NOT NULL为true非空.

所以:

SELECT * FROM t;
???????????????????
?   f1   ?   f2   ?
???????????????????
? (null) ?      1 ?
?      2 ? (null) ?
? (null) ? (null) ?
?      3 ?      4 ?
???????????????????
(4 rows)

SELECT * FROM t WHERE NOT (t IS NOT NULL);
???????????????????
?   f1   ?   f2   ?
???????????????????
? (null) ?      1 ?
?      2 ? (null) ?
? (null) ? (null) ?
???????????????????
(3 rows)
Run Code Online (Sandbox Code Playgroud)

  • 我在考虑`row_to_json()`,但这要好得多 (2认同)