簡體   English   中英

什么 Postgres 查詢可以擴展 json 列的數組並將數組中的每個字段作為一行?

[英]What Postgres query can i expand the array of json column and have each field in the array as a row?

[
   {
      "id":"193",
      "duration":0,
      "count":32,
      "date":"2021-11-10T18:55:00+05:30",
      "period_end":"2022-01-11T00:00:00.000Z",
      "started_at":"2021-11-10T18:55:00+05:30",
      "ended_at":"2021-11-10T19:45:00+05:30"
   }
]

我正在嘗試將這些 object 中的每一個鍵都排成一行。 誰能幫忙?

我認為每個作為一列會更有用,但你要求每個作為一行,所以你可以 UNION 一堆查詢,例如:

    select 1, info ->> 'id'
    from table1
    union
    select 2, info ->> 'duration'
    from table1
    union
    select 3, info ->> 'count'
    from table1
    union
    select 4, info ->> 'date'
    from table1
    union
    select 5, info ->> 'period_end'
    from table1
    union
    select 6, info ->> 'started_at'
    from table1
    union
    select 7, info ->> 'ended_at'
    from table1
    order by 1

Output:

    1   193
    2   0
    3   32
    4   2021-11-10T18:55:00+05:30
    5   2022-01-11T00:00:00.000Z
    6   2021-11-10T18:55:00+05:30
    7   2021-11-10T19:45:00+05:30

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM