mysql表字段json类型对json数组过滤该怎么操作?

mysql5.7表 添加了一个json的字段类型

当我们的json字符串类型为二维数组时,我该如何取其中符合要求的数据呢?

举例:

数据库 表 test

id         int     主键
json_text  json    json字符串
[
{
            "categoryId":105012,
            "templateId":0,
            "name":"酒水饮品",
            "parentId":0,
            "child":[
                {
                    "categoryId":105806,
                    "templateId":1086,
                    "name":"牛奶",
                    "parentId":105012,
                    "child":[

                    ]
                }
            ]
        },
        {
            "categoryId":106846,
            "templateId":0,
            "name":"火锅/串串香/麻辣烫/冒菜/香锅",
            "parentId":0,
            "child":[
                {
                    "categoryId":23,
                    "templateId":0,
                    "name":"配菜",
                    "parentId":106846,
                    "child":[

                    ]
                }
            ]
        },
        {
            "categoryId":48,
            "templateId":1103,
            "name":"蛋糕",
            "parentId":0,
            "child":[

            ]
        },
]

比如我获取test表json_text中所有的child字段,我的sql如下

select json_text->>'$[*].child' from test;

我要获取test表json_text中第一层categoryId为[48,105012] 里面的child数据该如何写这个sql呢?

阅读 6.6k
1 个回答

试下这个

select json_text ->> '$.child' from test where json_text -> '$.categoryId' in (48,105012);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题