当前位置:网站首页>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 isNULL
orpath
Parameter does not identify a value in the document , Then return to .json_doc
If the parameter is not valid JSON Document orpath
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 returnNULL
.json_doc
If the parameter is not valid JSON Documentation or anypath
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
边栏推荐
- February 20ctf record
- Professional things use professional people
- Reading and writing of nodejs excel (.Xlsx) files
- About the definition of pointer variables (personal notes)
- Only these four instructions are required to operate SQL data
- Interface learning
- Array introduction plus example 01
- Detailed summary of position positioning
- Drag modal box
- 投资理财产品的年限要如何选?
猜你喜欢
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
Instant messaging project (I)
The article is on the list. Welcome to learn
Matlab notes
How to add an external header file in vs?
CTFHub-rce
Dynamic programming example 2 leetcode62 unique paths
Extend the toolbar of quill editor
Semantic segmentation fcns in the wild: pixel level adaptive and constraint based adaptation
HR took the initiative to raise the salary of the test lady. How did she do it?
随机推荐
Database query optimization method
JMeter stress testing and agent recording
Object creation and invocation code example
CVPR2021-Semi-supervised Domain Adaptation based on Dual-level Domain Mixing for Semantic Segmentati
Keyboard key code value
C language -- Sanzi chess
Array: force deduction dichotomy
Go implements LRU cache
2022.1.25
Uva1103 ancient pictograph recognition
Small sample learning data set
Customize the console plot result style
Fundamentals of C language
Electronic Society C language level 1 28, character diamond
3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)
Interface learning
Go Concurrency
[day40 literature extensive reading] space and time in the child's mind: metallic or atomic
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.
2.20 learning content