Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

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呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

试下这个

select json_text ->> '$.child' from test where json_text -> '$.categoryId' in (48,105012);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...