针对 PostgreSQL 9.3 中的 JSON 列查询数组包含

Querying for array containment against a JSON column in PostgreSQL 9.3

我是 运行 PostgreSQL 9.3。我有一个名为 "model" 的列,类型为 "json" (not jsonb)。 "model" 列的内容类似于以下内容:

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

是否可以查询 model -> section -> colors 包含 "blue" 的行?

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