【问题标题】:Querying a nested JSON field in MySQL在 MySQL 中查询嵌套的 JSON 字段
【发布时间】:2020-02-21 14:04:52
【问题描述】:

我在 MySQL 中有一个 JSON 字段,其结构如下:

{
    "menu": {
        "reports": {
            "checked": 1
        }
    },
    "survey": {
        "5": {
            "checked": 1
        }
    },
    "location": {
        "208": {
            "checked": 1,
            "satisfied": 1
        }
    }
}

我想获取survey.5.checked=1 的记录。
以下工作正常:

select email,filters,filters->'$.survey' as survey from users

这很有效:

select email,filters,filters->'$.survey.reports' as reports from users

但这失败了:

select email,filters,filters->'$.survey.5' as survey from users

这失败了:

select email,filters,filters->'$.survey.5.checked' as survey from users

我如何在 JSON 字段中查询survey.5.checked = 1 的记录?您不能在 JSON 结构中使用数字作为键吗?

【问题讨论】:

    标签: mysql sql json


    【解决方案1】:

    您需要将全数字密钥双引号(同样,如果密钥包含空格,也需要双引号)。

    select email, filters, filters -> '$.survey."5".checked' from users
    

    如果你真的想过滤:

    select email, filters
    from users
    where filters -> '$.survey."5".checked' = 1
    

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      相关资源
      最近更新 更多