mysql從嵌套json中搜尋值
P粉366946380
P粉366946380 2023-09-06 14:49:48
0
1
507

我正在嘗試從 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學習者快速成長!