【问题标题】:Query of json arrays containing objects查询包含对象的json数组
【发布时间】:2021-03-19 01:29:44
【问题描述】:

它以下面的形式存储在 MySQL DB 中。

"config" 是一个 JSON 类型,它是一个对象数组。

我要查询“config”包含对象“type”为“download”的数据。

(即下例中带有“id=test2”和“id=test3”的数据)

我在执行包含 JSON 类型的查询时遇到了很多麻烦。

"id":"test1", "config":[{"type": "get", "request": {}}]
"id":"test2", "config":[{"type": "set", "request": {}}, {"type": "download", "request": {}}]
"id":"test3", "config":[{"type": "download", "request": {}}, {"type": "reset", "request": {}}]

【问题讨论】:

    标签: mysql sql arrays where-clause mysql-json


    【解决方案1】:

    您可以使用json_extract()json_contains() 做到这一点:

    select t.*
    from mytable t
    where json_contains(json_extract(config, '$[*].type'), '"download"')
    

    【讨论】:

      猜你喜欢
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      相关资源
      最近更新 更多