当前位置:网站首页>MySQL operation JSON

MySQL operation JSON

2022-06-25 05:37:00 Which floor do you rate moto

  • Table structure  

CREATE TABLE `jsonuse` (
  `data` json DEFAULT NULL,
  `id` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
 

  • JSON_OBJECT([KEY, val[, KEY, val] ...])

Calculate the list of key value pairs ( May is empty ), And return the... Containing these key value pairs JSON object . If any key name is NULL Or parameters are odd (JSON_OBJECT The parameters must be even ), An error will occur .

INSERT INTO jsonuse VALUES (JSON_OBJECT("name", " Wang Wu ", "email", "[email protected]", "age",18),2 );

Look at the inserted data :

  • JSON_KEYS(json_doc[, PATH]):

With JSON Array returns JSON The key in the top-level value of the object , If given path Parameters , Returns the top-level key in the selected path . return NULL If any parameter is NULL,
json_doc Parameters are not objects , perhaps path( If a given ) No object found . If json_doc Argument is not valid json file , perhaps path Parameter is not a valid path expression , Or include * or ** wildcard , An error will occur .
If the selected object is empty , The result array is empty . If the top-level value has nested sub objects , The return value does not include the keyframes from these sub objects .
SELECT * FROM jsonuse WHERE id = 4
SELECT JSON_KEYS(DATA,'$.b'),DATA  FROM jsonuse WHERE id = 4 ;

 SELECT JSON_KEYS(DATA),DATA  FROM jsonuse WHERE id = 4 ;

  • JSON_EXTRACT(json_doc, PATH[, PATH] ...) 

return JSON Data in documents , Select... From the document section that matches the path parameter . If any parameter is NULL Or the path of the value is not found in the document , Then return to NULL. If json_doc Argument is not valid json file , Or anything path None of the arguments are valid path expressions , An error will occur .

The return value consists of all values that match the path parameters . If these parameters may return multiple values , Then the matching value will be automatically wrapped as an array , The order corresponds to the path that generated them . otherwise , The return value is a single matching value .

Overall data :

SELECT JSON_EXTRACT(DATA, '$.name'),JSON_EXTRACT(DATA, '$.address') FROM jsonuse;

// Use ifnull Find all data It contains email Property line

// About mysql Function click this link

SELECT *  FROM jsonuse WHERE IFNULL (JSON_EXTRACT(DATA, '$.email'),NULL) IS NOT NULL

  • JSON_REMOVE(json_doc, PATH[, PATH] ...)

from JSON Delete data from the document and return results . If any parameter is NULL, Then return to NULL. If json_doc Argument is not valid json file , Or any path parameter is not a valid path expression , Or is it $, Or include * or ** wildcard , An error will occur .

Path parameters are evaluated from left to right . The document generated by calculating one path becomes the new value for calculating the next path .

If there is no element to delete in the document , It's not a mistake ; under these circumstances , The path does not affect the document .

Overall data :

SELECT JSON_REMOVE(DATA,'$.b')FROM jsonuse WHERE id = 4 ;

The values in the table are not actually deleted

  • JSON_LENGTH(json_doc[, path])

JSON_LENGTH(json_doc[, path]) return JSON The length of the document , perhaps , If  path The given parameters , Returns the length of the value in the document identified by the path .NULL If any parameter is  NULL or path  Parameter does not identify a value in the document , Then return to .json_doc If the parameter is not valid JSON Document or  path Parameter is not a valid path expression , An error will occur . stay MySQL 8.0.26 Before , If the path expression contains a * or  ** wildcard , It can also cause errors .

SELECT JSON_LENGTH(data) FROM jsonuse WHERE id = 1;

 JSON_SET(json_doc, path, val[, path, val] ...)

Members that do not exist in existing objects . The member is added to the object and associated with the new value .

Position beyond the end of an existing array . Expand the array with new values . If the existing value is not an array , It is automatically wrapped as an array , Then extend with the new value .

Available data

 SELECT JSON_SET(data,'$.age',21) FROM jsonuse WHERE id = 1;

 SELECT JSON_SET(data,'$.interesr','playCompuate') FROM jsonuse WHERE id = 1;

JSON_INSERT(json_doc, path, val[, path, val] ...): Insert values without replacing existing values .

  • Members that do not exist in existing objects . The member is added to the object and associated with the new value .

  • Position beyond the end of an existing array . Expand the array with new values . If the existing value is not an array , It is automatically wrapped as an array , Then extend with the new value .

JSON_REPLACE(json_doc, path, val[, path, val] ...).

  • Replace JSON The existing value in the document and return the result . If any parameter is NULL  be   return NULL.json_doc If the parameter is not valid JSON Documentation or any path Argument is not a valid path expression or contains   Or wildcard ,  An error will occur .

JSON_ARRAYAGG(col_or_expr) [over_clause] 

Aggregate the result set into a single  JSON Array , Its elements consist of rows . The order of the elements in this array is not defined . This function works on columns or expressions that evaluate to a single value .NULL If the result does not contain rows , Or something goes wrong , be   return .

over_clause from MySQL 8.0.14 Start , If there is  , This function will be executed as a window function .over_clause Such as The first 12.21.2 section ,“ Window function concept and syntax ” Described in .

SELECT JSON_ARRAYAGG(rolename) FROM sysrole

JSON_OBJECTAGG(key, value) [over_clause] 

Take two column names or expressions as arguments , The first one is used as a key , Second as value , And returns a that contains key value pairs JSON object .NULL If the result does not contain rows , Or something goes wrong , Then return to .NULL If any key name is or the number of parameters is not equal to 2, An error will occur .

over_clause from MySQL 8.0.14 Start , If there is , This function will be executed as a window function

 SELECT JSON_OBJECT(id,rolename)FROM sysrole

  added json Function reference :MySQL :: MySQL 8.0 Reference Manual :: 12.18.1 JSON Function Reference

原网站

版权声明
本文为[Which floor do you rate moto]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210504099870.html