mysql从嵌套json中搜索值
P粉366946380
P粉366946380 2023-09-06 14:49:48
0
1
506

我正在尝试从 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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!