MySQL JSON string processing

 For JSON string handling functions after MySQL5.7 support, the project uses two main functions:


1. JSON_EXTRACT (A, b)  Note: a field value is parsed desired, b is a value of expression begins with $   '$ *' to remove all JSON string '$ .key' to remove a specific value '$ [n] .key ' is removed so that a value JSON array.

 2.  JSON_UNQUOTE (value)  Note: This function is mainly used because of the functions in the extracted value "" as a character string, the function can be taken double quotes and converts to a string value.

Example: JSON string is

[{"form":"sj","name":"时间","type":"select","required":"true","value":"2020-02-14"},{"form":"remark","name":"备注","type":"text","required":"false","value":"上班"}}]

So we have to take the time necessary to json array so written 2020-02-14

SELECT JSON_UNQUOTE ( JSON_EXTRACT ( variant_value, '$[0].value' ) )  AS value  FROM tablename

3. After an additional value extracted in the sql JSON string is not used as a filter condition where statement, if it is worth the need to show the different results may be queried set as the sub-tables, with a function to display a different case Found method exemplary case have to use the function:

(1)select  case colum when '1' then '男' when '2' then '男'  else '..' end from ......

(2)select  case when  colum = '1'  then '男'  when colum = '2'  then '男'  else '..' end from ......

 

These are used in the function point to JSON have handled the primary method recorded in the sql in what has been for inspection.

Released six original articles · won praise 7 · views 1099

Guess you like

Origin blog.csdn.net/weixin_43814408/article/details/104308875