加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL嵌套JSON查询

发布时间:2020-12-13 16:30:20 所属栏目:百科 来源:网络整理
导读:在PostgreSQL 9.3.4上,我有一个名为“person”的JSON类型列,其中存储的数据格式为{dogs:[{breed:,name:},{breed:lt ;,名称:}]}.我想在索引0处检索狗的品种.以下是我运行的两个查询: 不起作用 db= select person-'dogs'-0-'breed' from people where id
在PostgreSQL 9.3.4上,我有一个名为“person”的JSON类型列,其中存储的数据格式为{dogs:[{breed:<>,name:<>},{breed:&lt ;>,名称:<>}]}.我想在索引0处检索狗的品种.以下是我运行的两个查询:

不起作用

db=> select person->'dogs'->>0->'breed' from people where id = 77;
ERROR:  operator does not exist: text -> unknown
LINE 1: select person->'dogs'->>0->'bree...
                                 ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

作品

select (person->'dogs'->>0)::json->'breed' from es_config_app_solutiondraft where id = 77;
 ?column?
-----------
 "westie"
(1 row)

为什么必须进行铸造?效率不高吗?我做错了什么或者这对postgres JSON支持是否必要?

这是因为操作符 – >>获取JSON数组元素作为文本.您需要使用强制转换将其结果转换回JSON.

您可以使用operator – >消除此冗余强制转换:

select person->'dogs'->0->'breed' from people where id = 77;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读