首頁  >  問答  >  主體

mysql從嵌套json中搜尋值

我正在嘗試從 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粉366946380P粉366946380410 天前510

全部回覆(1)我來回復

  • P粉554842091

    P粉5548420912023-09-07 09:42:13

    這個:

    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

    #

    回覆
    0
  • 取消回覆