sql >> Database teknologi >  >> RDS >> PostgreSQL

PostgreSql:Json Array til Rows ved hjælp af Lateral Join

Er det det, du vil have?

    -- just simulate table:
with my_table(details) as(
values
('{
"city": "London",
"name": "Sainburry",
"quantities": [112, 145, 222, 122, 124],
"prices": [4, 4, 4, 0, 3],
"dates": ["13.05.2020", "14.05.2020", "15.05.2020", "16.05.2020", "17.05.2020"]
}'::json)
)


-- here is query:
select  
my_table.details->>'city',  u.quantities, u.prices  
from my_table
JOIN LATERAL UNNEST( 
    ARRAY(SELECT json_array_elements_text(details->'quantities')) ,
    ARRAY(SELECT json_array_elements_text(details->'prices')) 
) u(quantities, prices) ON TRUE
WHERE
my_table.details->>'city' = 'London'

Se demo



  1. PostgreSQL Multi-Cloud Cluster Deployment

  2. MariaDB JSON_MERGE_PATCH() Forklaret

  3. MySQL "NOT IN" forespørgsel 3 tabeller

  4. Overfør data fra en database til en anden database