【问题标题】:Postgres jsob select specific array fieldsPostgres json 选择特定的数组字段
【发布时间】:2023-03-28 22:13:01
【问题描述】:

我有 postgreSQl 10.2 表“快照”,其中 jsonb 列名为“数据”

{
  "entries": [
    {
      "userName": "John",
      "age": "15"
    },
    {
      "userName": "Max",
      "age": "42"
    }]
{

需要查询以仅从数组中的条目中选择用户名。 我试过这个

select data->'entries'->>'userName' from snapshots;

但当然它不是返回我需要的值。

【问题讨论】:

    标签: json postgresql jsonb


    【解决方案1】:

    使用在横向连接中使用的函数jsonb_array_elements() 取消嵌套 json 数组:

    select item->>'userName'
    from snapshots
    cross join jsonb_array_elements(data->'entries') as item
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-16
      • 2021-08-16
      • 1970-01-01
      • 2017-12-27
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      相关资源
      最近更新 更多