select from … - based on a value in JSON format

烈酒焚心 提交于 2019-12-23 13:58:35

问题


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?


回答1:


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';


来源:https://stackoverflow.com/questions/17571019/select-from-based-on-a-value-in-json-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!