sql - 如何将数组 json 列中的值提取到 Postgresql 中的多行中?
问题描述
如何从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}]')
;
预期结果:
开始 | 结尾 |
---|---|
1 | 100 |
101 | 1000 |
1001 | 2000 |
2001年 | 2002年 |
解决方案
您可以jsonb_to_recordset
为此使用功能:
SELECT ranges."Start",
ranges."End"
FROM test_table,
jsonb_to_recordset(test_table.ranges) AS ranges("End" int, "Start" int)
推荐阅读
- c# - 通过反射设置表单的设计模式属性
- scala - 使用 mapN 验证的猫
- autodesk-forge - Forge BIM 360 参数值的 URL
- scala - 在使用 udf 进行操作时,将数据框中的空值保持为空
- javascript - 如何在javascript中分组并检测是否存在
- javascript - 根据索引反应setState多个数组
- python - Python函数中的嵌入式SQL(SQLite)提供错误的输出
- python-3.x - 如何在底图投影上绘制散点图?
- python - 无法使用 predict() 函数预测 ARIMA 中给定输入日期的值
- php - 我修改后的 php 邮件功能不起作用