【问题标题】:PostgreSQL: How to do a select query on a json column that contains array or arraysPostgreSQL:如何对包含数组或数组的 json 列进行选择查询
【发布时间】:2020-04-24 20:09:55
【问题描述】:

如果我运行以下查询:

select headers from database_name.schema_name.table;

输出是这样的 JSON 数组:

如何查询 PostgreSQL 12 以仅返回“From”节点?

预期结果:

*From*
Me
Him
His

【问题讨论】:

  • 您确定您使用的是 Postgres 吗?因为from db.all_messages.message 在 Postgres 中无效
  • 是的,PostgreSQL 12 很抱歉,它应该是“从 database_name.schema_name.table 中选择标头;”。我已经更改了帖子以反映这一点。
  • Postgres 不允许引用这样的数据库。

标签: arrays json postgresql


【解决方案1】:

您需要取消嵌套数组,然后通过索引访问元素:

select x.h ->> 1 as "From"
from t
  cross join jsonb_array_elements(t.headers) as x(h)
where x.h ->> 0 = 'From'  

Online example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 2018-06-09
    • 2021-08-07
    • 2016-12-05
    相关资源
    最近更新 更多