2017-11-10 132 views
1

我正在运行PostgreSQL 9.3。我有一个名为“model”的列“json”(而不是 jsonb)。 “模型”列的内容如下所示:在PostgreSQL中查询针对JSON列的数组遏制9.3

{ "section": { "colors": ["red", "blue", "green"] } }

是否可以查询,其中包含model -> section -> colors“蓝色”行?

回答

1

9.3中没有json比较运算符。您可以调用函数json_array_elements()并将函数的结果转换为textDistinct on中情况下使用的JSON数组包含重复元件:

select distinct on (id) t.* 
from my_table t, 
json_array_elements(model->'section'->'colors') 
where value::text = '"blue"'; 

SqlFiddle

+0

这是正确的答案。我最终迁移到9.4版。 – tambler