PostgreSQL JsonB 根据对象属性查询 JSON 数组中的对象
PostgreSQL JsonB query for object in JSON array based on object attributes
所以我已经在 Whosebug 上看到了对这个问题的一些其他回复,但其中 none 对我有用。
{
"data": {
"list": [
{"id": "2ac5bf6f-bc4a-49e8-8f9f-bc518a839981", "type": "type1"},
{"id": "d15ac090-11ce-4c0c-a05d-d4238f01e8b0", "type": "type3"},
{"id": "b98958fa-87c4-4dcc-aa84-beaf2b32c5c0", "type": "type1"},
{"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"},
{"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"},
{"id": "0f7f4ced-61c2-45da-b15c-0e12058f66a7", "type": "type4"}
]
}
}
这个Json存储在一个叫"questions"的字段中,现在我想查询这个table列表中某个id的对象。所以说我有id 555816da-4547-4a82-9e7e-1e92515bd82b
,我想return
{"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"}
我在网上看到的解决方法(主要是在这里),但没有奏效:
SELECT questions->'data'->'list'
FROM assignments
WHERE id='81asd6230-126d-4bc8-9745-c4333338115c'
AND questions->'data'->'list' @> '[{"id":"854f4d2a-f37c-42cb-9a1f-17a15454a314"}]';
我已经在多个不同的响应中看到了这个解决方案,但它根本没有缩小数组的范围,它每次都 return 是完整的。 where子句中的第一个id是我想要的具体赋值对象的id,这里基本无关
SELECT questions->'data'->'list'
FROM assignments
WHERE id='81asd6230-126d-4bc8-9745-c4333338115c'
AND questions->'data'->'list' @> '[{"id":"854f4d2a-f37c-42cb-9a1f-17a15454a314"}]';
这个return没什么。
有没有人知道如何轻松地做到这一点?
您可以使用函数 jsonb_array_elements(jsonb)
来 select json 数组的所有元素:
select jsonb_array_elements(questions->'data'->'list') elem
from assignments
where id='81asd6230-126d-4bc8-9745-c4333338115c'
elem
-----------------------------------------------------------------
{"id": "2ac5bf6f-bc4a-49e8-8f9f-bc518a839981", "type": "type1"}
{"id": "d15ac090-11ce-4c0c-a05d-d4238f01e8b0", "type": "type3"}
{"id": "b98958fa-87c4-4dcc-aa84-beaf2b32c5c0", "type": "type1"}
{"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"}
{"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"}
{"id": "0f7f4ced-61c2-45da-b15c-0e12058f66a7", "type": "type4"}
(6 rows)
如果您想要 select 具有特定 id
:
的元素,请使用上述查询
select elem
from (
select jsonb_array_elements(questions->'data'->'list') elem
from assignments
where id='81asd6230-126d-4bc8-9745-c4333338115c'
) sub
where elem->>'id' = '854f4d2a-f37c-42cb-9a1f-17a15454a314'
elem
-----------------------------------------------------------------
{"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"}
(1 row)
所以我已经在 Whosebug 上看到了对这个问题的一些其他回复,但其中 none 对我有用。
{
"data": {
"list": [
{"id": "2ac5bf6f-bc4a-49e8-8f9f-bc518a839981", "type": "type1"},
{"id": "d15ac090-11ce-4c0c-a05d-d4238f01e8b0", "type": "type3"},
{"id": "b98958fa-87c4-4dcc-aa84-beaf2b32c5c0", "type": "type1"},
{"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"},
{"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"},
{"id": "0f7f4ced-61c2-45da-b15c-0e12058f66a7", "type": "type4"}
]
}
}
这个Json存储在一个叫"questions"的字段中,现在我想查询这个table列表中某个id的对象。所以说我有id 555816da-4547-4a82-9e7e-1e92515bd82b
,我想return
{"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"}
我在网上看到的解决方法(主要是在这里),但没有奏效:
SELECT questions->'data'->'list'
FROM assignments
WHERE id='81asd6230-126d-4bc8-9745-c4333338115c'
AND questions->'data'->'list' @> '[{"id":"854f4d2a-f37c-42cb-9a1f-17a15454a314"}]';
我已经在多个不同的响应中看到了这个解决方案,但它根本没有缩小数组的范围,它每次都 return 是完整的。 where子句中的第一个id是我想要的具体赋值对象的id,这里基本无关
SELECT questions->'data'->'list'
FROM assignments
WHERE id='81asd6230-126d-4bc8-9745-c4333338115c'
AND questions->'data'->'list' @> '[{"id":"854f4d2a-f37c-42cb-9a1f-17a15454a314"}]';
这个return没什么。
有没有人知道如何轻松地做到这一点?
您可以使用函数 jsonb_array_elements(jsonb)
来 select json 数组的所有元素:
select jsonb_array_elements(questions->'data'->'list') elem
from assignments
where id='81asd6230-126d-4bc8-9745-c4333338115c'
elem
-----------------------------------------------------------------
{"id": "2ac5bf6f-bc4a-49e8-8f9f-bc518a839981", "type": "type1"}
{"id": "d15ac090-11ce-4c0c-a05d-d4238f01e8b0", "type": "type3"}
{"id": "b98958fa-87c4-4dcc-aa84-beaf2b32c5c0", "type": "type1"}
{"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"}
{"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"}
{"id": "0f7f4ced-61c2-45da-b15c-0e12058f66a7", "type": "type4"}
(6 rows)
如果您想要 select 具有特定 id
:
select elem
from (
select jsonb_array_elements(questions->'data'->'list') elem
from assignments
where id='81asd6230-126d-4bc8-9745-c4333338115c'
) sub
where elem->>'id' = '854f4d2a-f37c-42cb-9a1f-17a15454a314'
elem
-----------------------------------------------------------------
{"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"}
(1 row)