2016-06-22 12 views
1

Ich habe Tabelle zu filtern:Wie mit diesen Daten JSONB Liste

id  | 1 
accounts | [{"id": "100", "properties": [{"id": "PR-001", "name": "name1"}, {"id": "PR-002", "name": "name2"}]}] 
property | "PR-001" 

accounts jsonb Feld ist.

Ich muss alle property.name erhalten, wobei accounts.property.id gleich Eigenschaft mit SELECT ist.

ich Postgres ist mit 9,5

+0

diese funktioniert :) ist es gut? 'DISTINCT x.properties SELECT - >> 'name' FROM my_table t, ( SELECT json_array_elements (json_array_elements (Konten :: JSON) -> 'Eigenschaften') AS Eigenschaften VON my_table ) x WHERE x.properties - >> 'id' = t.property; ' – HoTicE

Antwort

1

Sie linkslaterale JOIN verwenden:

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) 
Verwandte Themen