简体   繁体   English

MySQL 使用“like”或正则表达式搜索 JSON 数组的值

[英]MySQL searching JSON array for value, using `like` or regexp

I have a table with a field called data_json .我有一个名为data_json的字段的表。

An example of the JSON shape is below: JSON 形状的示例如下:

{
  title: "My Title",
  arr: ["hello there", "foobar", "foo hello bar"]
}

I'd simply like to find rows where data_json->'$.arr' contains a value, using regexp , or like .我只想使用regexplike查找data_json->'$.arr'包含值的行。

Eg:例如:

select * from mytable where ??? like '%hello%';

Need to extract the elements of the array.需要提取数组的元素。 To perform this JSON_EXTRACT() function might be used along with index values upto the length of it.要执行此JSON_EXTRACT()函数,可能会与索引值一起使用,直到它的长度。 Row generation methods through metadata tables of information_schema , such like below, might be used to generate those index values :通过information_schema元数据表的行生成方法,如下所示,可用于生成这些索引值:

SELECT value
  FROM 
  (
    SELECT  @i := @i + 1 AS rn,
           JSON_UNQUOTE(JSON_EXTRACT(data_json, CONCAT('$.arr[',@i-1,']'))) AS value              
      FROM information_schema.tables 
     CROSS JOIN mytable 
     CROSS JOIN (SELECT @i := 0) r
  ) q
 WHERE value LIKE '%hello%'

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM