代码之家  ›  专栏  ›  技术社区  ›  tambler

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

  •  1
  • tambler  · 技术社区  · 7 年前

    我正在运行PostgreSQL 9.3。我有一个名为“model”的列,类型为“json”(

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

    可以查询其中的行吗 model -> section -> colors 包含“蓝色”?

    1 回复  |  直到 7 年前
        1
  •  1
  •   klin    7 年前

    9.3中没有json比较运算符。您可以调用函数 json_array_elements() 并将函数的结果转换为 text . Distinct on 用于json数组包含重复元素的情况:

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

    SqlFiddle