【发布时间】:2021-07-28 10:17:09
【问题描述】:
我在具有以下结构的表之一中有一个 jsonb 列:
{
"3424": {
"status": "pending",
"remarks": "sample here"
},
"6436": {
"status": "new",
"remarks": "sample here"
},,
"9768": {
"status": "cancelled",
"remarks": null,
"by": "customer"
}
}
我正在尝试创建一个视图,将状态放在各个列中,键是它们的值:
pending | new | cancelled | accepted | id | transaction
3424 | 6436 | 9768 | null | 1 | testing
问题是密钥是动态的(数字并对应于某个 id),所以我无法确定使用此处所述功能/操作的确切密钥:https://www.postgresql.org/docs/9.5/functions-json.html
我已经阅读了json_path_query 并且能够在不需要知道密钥的情况下提取这里的状态,但我还不能将它与整数密钥结合起来。
select mt.id, mt.transaction, hstatus from mytable mt
cross join lateral jsonb_path_query(mt.hist, '$.**.status') hstatus
where mt.id = <id>
但这现在将状态返回为行。我对 (postgre)sql 很陌生,所以我只到了这一步。
【问题讨论】:
-
如果您有两个带有
pending的条目怎么办?还是三个cancelled?
标签: postgresql jsonb