如何过滤 JSONB 列表
How to filter JSONB list
我有 table 这个数据:
id | 1
accounts | [{"id": "100", "properties": [{"id": "PR-001", "name": "name1"}, {"id": "PR-002", "name": "name2"}]}]
property | "PR-001"
accounts
为jsonb字段
我需要得到所有 property.name 其中 accounts.property.id 等于 属性 和 SELECT.
我正在使用 Postgres 9.5
您可以使用 LEFT LATERAL JOIN:
WITH tbl (id,accounts,property) AS (
SELECT 1, '{"id": "100", "properties": [{"id": "PR-001", "name": "name1"}, {"id": "PR-002", "name": "name2"}]}'::jsonb, 'PR-001'::text
)
SELECT t.id, acc->>'name'
FROM tbl t
LEFT JOIN LATERAL jsonb_array_elements(t.accounts->'properties') acc ON (acc->>'id' = t.property)
我有 table 这个数据:
id | 1
accounts | [{"id": "100", "properties": [{"id": "PR-001", "name": "name1"}, {"id": "PR-002", "name": "name2"}]}]
property | "PR-001"
accounts
为jsonb字段
我需要得到所有 property.name 其中 accounts.property.id 等于 属性 和 SELECT.
我正在使用 Postgres 9.5
您可以使用 LEFT LATERAL JOIN:
WITH tbl (id,accounts,property) AS (
SELECT 1, '{"id": "100", "properties": [{"id": "PR-001", "name": "name1"}, {"id": "PR-002", "name": "name2"}]}'::jsonb, 'PR-001'::text
)
SELECT t.id, acc->>'name'
FROM tbl t
LEFT JOIN LATERAL jsonb_array_elements(t.accounts->'properties') acc ON (acc->>'id' = t.property)