簡體   English   中英

如何將數組 json 列中的值提取到 Postgresql 中的多行中?

[英]How to extract values from array json column into multiple rows in Postgresql?

如何從ranges列中的 json arrays 中提取值作為多行 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}]')
;

預期結果:

開始 結尾
1 100
101 1000
1001 2000
2001年 2002年

您可以為此使用jsonb_to_recordset function:

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

暫無
暫無

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

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