简体   繁体   中英

select from … - based on a value in JSON format

Let's say I have a database table with several common columns such as name, gender, age, ...

Besides I have an additional column using the JSON datatype (available from Postgres 9.2) with arbitrary length and arbitrary fields in JSON:

{"occupation":"football"}

{"occupation":"football", "hair-colour":"black"}

{"hair-style":"curly"}

Using new features of Postgres 9.3 I want to return all rows with occupation = football for instance.

Something like this pseudo: select * from table where json_field.occupation = football

Is there a way of doing this?

If I understood the manual correctly, you can access JSON fields with -> and ->> operators. The query would look like:

SELECT *
FROM your_table
WHERE json_field ->> 'occupation' = 'football';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM