简体   繁体   中英

Mysql 5.7 search values inside a json column

How can I search the value "ABCD" inside two JSON array in a single column? This is multiple JSON data within square bracket Sample JSON data added below :

[{"id": "ABCD", "TEST1": "2002", "value": "test value1", "comment": ""}, {"id": "ABCD", "TEST2": "2003", "value": "test value2", "comment": ""}]

You can use Json_Search() function. In this function, we can set the second argument to one or all . We will use it as all to search for all the keys containing the given value.

SELECT JSON_SEARCH(json_column_name, 'all', 'ABCD');

DEMO - Schema (MySQL v5.7)

SET @json_column_name = '[{"id": "ABCD", "TEST1": "2002", "value": "test value1", "comment": ""}, {"id": "ABCD", "TEST2": "2003", "value": "test value2", "comment": ""}]';

SELECT JSON_SEARCH(@json_column_name, 'all', 'ABCD') AS result;

RESULT

| result                 |
| ---------------------- |
| ["$[0].id", "$[1].id"] |

View on DB Fiddle

You can follow the mysql documentation

mysql> SELECT JSON_SEARCH(@j, 'all', 'abc');

Here you can search whatever you want.

Document:

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