首页  >  问答  >  正文

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 天前515

全部回复(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
  • 取消回复