当前位置:网站首页>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
pathThe given parameters , Returns the length of the value in the document identified by the path .NULLIf any parameter isNULLorpathParameter does not identify a value in the document , Then return to .json_docIf the parameter is not valid JSON Document orpathParameter 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
NULLbe returnNULL.json_docIf the parameter is not valid JSON Documentation or anypathArgument 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 .
NULLIf the result does not contain rows , Or something goes wrong , be return .
over_clausefrom MySQL 8.0.14 Start , If there is , This function will be executed as a window function .over_clauseSuch 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
边栏推荐
- I got to know data types and function variables for the first time. I learned data types and function variables together today and yesterday, so I saved them in the first issue to record.
- JS function to realize simple calculator
- Notes on non replacement elements in the line (padding, margin, and border)
- 2022.1.23 diary
- February 19 CTF exercise
- SSRF-lab
- Penetration test - right raising topic
- For in JS Of and for in
- [Huawei machine test] hj16 shopping list
- Go Context - Cancelation and Propagation
猜你喜欢

UVA816 Abbott’s Revenge

"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare

Code learning-cvpr2020 unsupervised domain adaptive semantic segmentation: intra advance

3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)

Instant messaging project (I)
![H5 canvas drawing circle drawing fillet [detailed explanation]](/img/6f/a33a323b6cd0918066e8b71a22d841.jpg)
H5 canvas drawing circle drawing fillet [detailed explanation]

The article is on the list. Welcome to learn

2.20 learning content

Depth of binary tree

Dynamic programming example 2 leetcode62 unique paths
随机推荐
IronOCR 2022.1 Crack
Personalized Federated Learning with Moreau Envelopes
Depth of binary tree
A review of small sample learning
JSON Library Tutorial from scratch (III): parsing strings, learning and sorting notes
Small sample learning data set
Enhanced paste quill editor
Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
电子协会 C语言 1级 28 、字符菱形
Detailed summary of float
Duplicate symbols for architecture i386 clang
Drag modal box
2021-03-23
Read the general components of antd source code
Array introduction plus example 01
Vue uses keep alive to cache page optimization projects
Semantic segmentation cvpr2020 unsupervised intra domain adaptation for semantic segmentation through self supervision
1.6.3 use tcpdump to observe DNS communication process
ArcGIS Engine + Visual Studio installation tutorial
Characteristics of ES6 arrow function








