mysql从嵌套json中搜索值
P粉366946380
P粉366946380 2023-09-06 14:49:48
[MySQL讨论组]

我正在尝试从 mysql 列中的嵌套 json 中搜索 userId 值 22

我的json是

'{
     "data": [
            {"calendarId":"11","userId": "12"},
            {"calendarId":"21","userId": "22"}
           ]
    }'

我尝试了以下语法:

1. where JSON_EXTRACT(column_field,'$.userId') = 22

2. where
JSON_EXTRACT(
column_field,
'$.data[*].userId'
) = 22
  1. 也尝试过使用 JSON_Table ,但没​​有在 where 条件下获得精确的嵌套 json 值。

P粉366946380
P粉366946380

全部回复(1)
P粉554842091

这个:

select json_extract('{
     "data": [
            {"calendarId":"11","userId": "12"},
            {"calendarId":"21","userId": "22"}
           ]
    }','$[0].data[*].userId');

给出:[“12”,“22”]

还有这个:

select * 
from json_table(json_extract('{"data": [{"calendarId":"11","userId": "12"},{"calendarId":"21","userId": "22"}]}',
                '$[0].data[*].userId'), 
                '$[*]' columns (value int path "$")) x
;

给出:

12
22

添加 WHERE 子句,仅查找值 22 应该不是问题。

附注以上是使用MySQL 8.x测试的,参见:DBFIDDLE

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号