如何使用PostgreSQL查找任何列中具有NULL值的所有行 [英] How to find all rows with a NULL value in any column using PostgreSQL

查看:826
本文介绍了如何使用PostgreSQL查找任何列中具有NULL值的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多稍微相似的问题,但没有一个能够完全解决这个问题。 查找任何列中具有空值的所有行是我能找到的最接近的一个,并且提供了SQL Server的答案,但我正在寻找一种在PostgreSQL中实现此目的的方法。

There are many slightly similar questions, but none solve precisely this problem. "Find All Rows With Null Value(s) in Any Column" is the closest one I could find and offers an answer for SQL Server, but I'm looking for a way to do this in PostgreSQL.

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

How can I select only the rows that have NULL values in any column?

我可以很容易地获得所有列名:

I can get all the column names easily enough:

select column_name from information_schema.columns where table_name = 'A';

尚不清楚如何检查多个列名称是否为NULL值。显然这是行不通的:

but it's unclear how to check multiple column names for NULL values. Obviously this won't work:

select* from A where (
  select column_name from information_schema.columns where table_name = 'A';
) IS NULL;

然后搜索并没有发现任何有用的东西。

And searching has not turned up anything useful.

推荐答案

您可以使用 NOT(< table> IS NOT NULL)

来自文档


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

If the expression is row-valued, then IS NULL is true when the row expression itself is null or when all the row's fields are null, while IS NOT NULL is true when the row expression itself is non-null and all the row's fields are non-null.

所以:

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)

这篇关于如何使用PostgreSQL查找任何列中具有NULL值的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆