使用MySQL返回JSON字符串中的条目

I'm storing data as a JSON string in mysql because it comes with a lot of advantages in my situation. These JSON strings can get quite large and within the JSON are other entries (with unique id's).

At the moment I'm fetching the whole JSON string and then loop through it to find a particular entry by id.

Is there a way that I can fetch only the entry I'm looking for in the JSON string with MySQL only? So that I don't have to fetch the whole JSON string and loop through it.

use common_schema. Here’s an example: in the first i set variable that have a json format, assume this is your json in your record field table.

SET @json = '{"id":"1","name":"number one"}';

and then parsing that variable (your field) into two separate columns with common_schema using extract_json_value() function:

select common_schema.extract_json_value(@json,'/id') as ID,
    common_schema.extract_json_value(@json,'/name') as NAME

Ouput

+----------+-----------+
| ID       | NAME      |
+----------+-----------+
| 1        | number one|
+----------+-----------+

So you can implement in your table Query with CONDITIONS STATEMENT. this example Query (JSON in field3):

SELECT field1, field2, field3 as JSON,
       common_schema.extract_json_value(field3, '//sub1') as SUB1,
       common_schema.extract_json_value(field3, '//sub2') as SUB2
FROM your_table
WHERE common_schema.extract_json_value(field3, '//sub1') = 'YOUR_CONDITION'