我在postgres中有一个表,在名为datajsonb列中有如下行:

{ "block": { "data": null, "timestamp": "1680617159" } }
{"block": {"hash": "0xf0cab6f80ff8db4233bd721df2d2a7f7b8be82a4a1d1df3fa9bbddfe2b609e28", "size": "0x21b", "miner": "0x0d70592f27ec3d8996b4317150b3ed8c0cd57e38", "nonce": "0x1a8261f25fc22fc3", "number": "0x1847", "uncles": [], "gasUsed": "0x0", "mixHash": "0x864231753d23fb737d685a94f0d1a7ccae00a005df88c0f1801f03ca84b317eb", "gasLimit": "0x1388", "extraData": "0x476574682f76312e302e302f6c696e75782f676f312e342e32", "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "stateRoot": "0x2754a138df13677ca025d024c6b6ac901237e2bf419dda68d9f9519a69bfe00e", "timestamp": "0x55baa522", "difficulty": "0x3f5a9c5edf", "parentHash": "0xf50f292263f296897f15fa87cb85ae8191876e90e71ab49a087e9427f9203a5f", "sealFields": [], "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", "receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", "transactions": [], "totalDifficulty": "0x239b3c909daa6", "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"}, "transaction_receipts": []}

我想要编写一个SQL查询,它 Select 值为block.data的所有行,但不 Select 没有data字段的行.

我试过以下几种方法,但都失败了:

SELECT * FROM table WHERE data->>'block'->>'data' IS NULL;
SELECT * FROM table WHERE data->'block'->'data' IS NULL;
SELECT * FROM table WHERE jsonb_extract_path_text(data, 'block', 'data') IS NULL;

似乎在所有这些情况下,如果data字段不存在,它就会通过where条款.

推荐答案

只需在以下条件下使用'null'::jsonb:

select *
from my_table
where data->'block'->'data' = 'null'

其他答案,无论多么有效,都是不必要的复杂,掩盖了事物的本质.问题是,'null'::jsonb与Postgres null不同:

select 'null'::jsonb is null;

 ?column?
----------
 f
(1 row)

您可以使用简单的btree索引来支持第一个查询:

create index on my_table ((data->'block'->'data'));

或者,杜松子wine 指数可以支持@>运算符:

create index on my_table using gin ((data->'block'->'data'));

select *
from my_table
where data->'block'->'data' @> 'null';

Postgresql相关问答推荐

Postgs SQL用于比较两个表之间的数据并填充到目标中

使用regexp获取表名

如何在PostgreSQL中对深度嵌套的jsonb属性创建索引

在Axum 0.5中,如何在一个请求处理程序中同时使用数据库和路径解析?

需要用 jack/pgx 更新 golang 中复合类型的 PSQL 行

这个 Supbase 查询在 SQL ( Postgresql ) 中的类似功能是什么

postgres 如何计算多列哈希?

包含间隔在 jooq 和 postgres 中没有按预期工作

如何在 PostgreSQL 的回归测试中测试 TYPE 发送和接收函数

为什么这里的默认格式不同? (ISO 8601 的两种不同实现)

PostgreSQL SELECT 结果具有不同的 id,它更喜欢来自另一个表的特定值

JPA findBy 字段忽略大小写

org.postgresql.util.PSQLException:错误:relation "app_user" does not exist

从 Postgres 数据库中删除所有函数

如何使用 postgresql 中的存储过程将数据插入表中

如何在数据库表中查找重复条目?

在 PostgreSQL 中跳过每 n 个结果行

错误:ERROR: table name specified more than once

如何在 JPA 中使用 Postgres JSONB 数据类型?

如何为本地 Rails 元素设置 Postgres 数据库?