【问题标题】:How to extract values from array json column into multiple rows in Postgresql?如何将数组 json 列中的值提取到 Postgresql 中的多行中?
【发布时间】:2021-02-11 23:09:53
【问题描述】:

如何从 ranges 列中的 json 数组中提取值作为多行 Postgresq?

CREATE TABLE test_table (
  id INTEGER, 
  ranges jsonb
);

INSERT INTO test_table(id, ranges) VALUES
(1,'[{"End": 100, "Start": 1}, {"End": 1000, "Start": 101}]'),
(2,'[{"End": 2000, "Start": 1001}, {"End": 2002, "Start": 2001}]')
;

预期结果:

Start End
1 100
101 1000
1001 2000
2001 2002

【问题讨论】:

    标签: sql json postgresql jsonb


    【解决方案1】:

    您可以为此使用jsonb_to_recordset 函数:

    SELECT ranges."Start",
           ranges."End"
    FROM   test_table,
           jsonb_to_recordset(test_table.ranges) AS ranges("End" int, "Start" int)
    

    http://www.sqlfiddle.com/#!17/22d62/10

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2020-11-03
      相关资源
      最近更新 更多