为什么我的JSONB查询没有返回任何结果

时间:2018-06-17 08:51:26

标签: postgresql postgresql-json

查看我的JSONB字段

select internet_address from resporg_account_ids where id=3
=> [{"zone": "us-central1", "address": "127.0.0.1", "reserve": true}, {"zone": "us-east1", "address": "127.0.0.10", "reserve": true}

相关的jsonb查询

select * from resporg_account_ids where internet_address <@ '[{"zone": "us-central1", "address": "127.0.0.1", "reserve": true}]'::jsonb;

和这个

select * from resporg_account_ids where internet_address <@ '{"zone": "us-central1", "address": "127.0.0.1", "reserve": true}'::jsonb;

我的输出控制台中没有结果。

另外,我想根据个人密钥查询...

select * from resporg_account_ids where internet_address->>address="127.0.0.1" and internet_address->>reserve=true

最后,

jsondb=# select version();
                                                    version
---------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.10 on x86_64-apple-darwin17.2.0, compiled by Apple LLVM version 9.0.0 (clang-900.0.38), 64-bit
(1 row)

注意:

第1部分:解决了,感谢@mroman指出了这一点。

2 个答案:

答案 0 :(得分:0)

您应该对对象的json键使用单引号:

select * from resporg_account_ids where internet_address->>'address'='127.0.0.1'

在第二部分中,您尝试将文本与布尔值进行比较,因为->>返回文本。所以你需要施放类型:

... and (internet_address->>'reserve')::boolean is true

答案 1 :(得分:0)

CTE来救援......

with intaddr as (select jsonb_array_elements(internet_address) internetaddr from resporg_account_ids)
select * from intaddr where internetaddr->>'address' = '127.0.0.1' and (internetaddr->>'reserve')::boolean=true;