简体   繁体   中英

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

I have a table with a field called data_json .

An example of the JSON shape is below:

{
  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 .

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. Row generation methods through metadata tables of information_schema , such like below, might be used to generate those index values :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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