我正在使用 PostgreSQL 查询这个名为
attributes
的 json 字段:
{
"rule": {
"requisite": [
{
"link": {
object: "a",
description: "I'm the A object"
}
},
{
"link": {
object: "b",
description: "I'm the B object"
}
},
{
"link": {
object: "c",
description: "I'm the C object"
}
}
]
}
}
如果我运行以下请求,使用不同的
->0
、->1
、->2
运算符,我可以一一访问其索引处的每个单个数组项:
WITH rules AS (
SELECT attributes->'rule'->'requisite'->0->'link'->'object' AS object
FROM mytable
)
SELECT * FROM rules
我可以有
a
,或b
或c
。我搜索过类似
->*
,jsonb_array_elements(attributes->'rule'->'requisite')
object
------
a
b
c
是的,@Bergi,你是对的。
我最终成功了。
WITH requisites AS (
SELECT attributes->'rule'->'requisite' AS requisite
FROM mytable
)
select jsonb_array_elements(requisite)->'link'->'object' from requisites