【问题标题】:how to get data from jsonb column in postgres?如何从 postgres 的 jsonb 列中获取数据?
【发布时间】:2020-09-16 11:59:27
【问题描述】:

我有一个数据库,它有一个 table。我的表 id 中有两列,以及类型的描述 jsonb 现在我想获取表中的所有 name。如何做到这一点

我试过这样但没有用

SELECT description::jsonb->name FROM Category 

预期输出

["hello","test","world"]

【问题讨论】:

    标签: sql arrays json postgresql postgresql-9.4


    【解决方案1】:

    您可以使用聚合:json[b]_agg() 可用于生成一个 json 数组,该数组由原始对象的所有 'name' 属性组成,按 id 排序。

    select jsonb_agg(description ->> 'name' order by id) res from category
    

    请注意,由于 description 的开头数据类型为 jsonb,因此不需要额外的强制转换。

    【讨论】:

    • 是的,它工作正常..!!谢谢你的帮助。我还需要一个小帮助。我可以只显示nametest 的人吗?
    • 使用这个链接我发现了一些东西postgresql.org/docs/9.5/functions-json.html
    • 我可以用这个jsonb_extract_path_text
    • 只查找名称为test的人
    • @user944513 select jsonb_agg(description ->> 'name' order by id) res from category where (description ->> 'name')='test'
    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2016-03-09
    • 2018-03-11
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多