当前位置:网站首页>MySQL JSON data types & functions
MySQL JSON data types & functions
2022-06-29 20:26:00 【Bald programmer Xiao Wang】
One 、mysql Medium JSON type
MySQL :: MySQL 8.0 Reference Manual :: 11.5 The JSON Data Type
1.2 json Medium path Express
MySQL Through strings Path Descriptions help us map to the corresponding data . and JavaScript The operations of objects in are similar , adopt . Get the properties of the next level , adopt [] Get array elements .
The difference is that you need to go through $ To express oneself , It's easy to understand . In addition, you can use * and ** Two wildcards , such as .* Represents the values of all members of the current hierarchy ,[*] Represents all member values in the current array .** similar LIKE You can also use prefixes and suffixes , such as a**b It means by a start ,b The path to the end .
SET @j = '[{"a": 1, "b": 2, "c": {"d": 4}},{"a":2},{"abb":3}]';
select JSON_EXTRACT(@j, '$[0].a'), JSON_EXTRACT(@j, '$[*].a');
mysql Chinese vs json Function summary of :

Two 、 establish json The value of the function
2.1 JSON_ARRAY([val[, val] ...])
The parameter for n It's worth , A reference to n Composed of two values json Array

2.2 JSON_OBJECT([key, val[, key, val] ...])
The parameter for n Key value pairs , The output parameter is composed of these key value pairs json object

Will a string The string reference is json value , By wrapping around the string '"' Double quotes , And transfer the escape character in the string . This function is usually used to create a json Legal in the document json character string .

You can also cast other types of fields to json type :

2.5 JSON_ARRAYAGG(col_or_expr) [over_clause] And JSON_OBJECTAGG(key, value) [over_clause]
Two aggregate functions can also generate json value , among JSON_ARRAYAGG Wrap the elements contained in the data column into json array return

JSON_OBJECTAGG Enter the reference , For two column names , The first column value is key, The second column value is value. Return to key:value Generated key value pairs .

3、 ... and 、 retrieval json Function of
3.1 JSON_CONTAINS(target, candidate[, path])
Detection target json object (target) Whether there are candidates in the json object (candidate). If you pass in path Parameters , Detection target The specific path Whether the location contains candidates json object . The rules for determining whether to include are as follows :
1) The goal is json Objects and candidates json Objects are scalars , When two scalars are comparable 【 Have the same JSON_TYPE】 And at the same time , The goal is json Object contains candidate json object .
2) The goal is json Objects and candidates json Objects are json Array , Then as a candidate json When each element of the object is contained by some element of the target object , The goal is json Object contains candidate json object
3) The goal is json The object is json Array , The candidate json Object is not json Array , When the candidate object is contained by some elements of the target object , The goal is json Object contains candidate json object
4) The goal is json Objects and candidates json Objects are json object. When the candidate object's key Are contained by the target object , The associated value is cut by the target object key When the associated value contains , The goal is json Object contains candidate json object .
5) The goal is json Object is not json Array , The candidate json The object is json Array , The goal is json An object cannot contain candidates json object .

3.2 JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)
testing json_doc Whether it contains the formulation path .
one:json_doc Contains any path
all:json_doc Contains all paths

3.3 JSON_EXTRACT(json_doc, path[, path] ...)
return json Documents in the corresponding path in , Return all documents of the corresponding path , If there are multiple packages json array return
3.4 column->path
JSON_EXTRACT The grammar sugar of , If JSON_EXTRACT There are only two parameters , There is only one path, You can use this grammar instead of sugar
select wg->'$[*][0].a' as wa from table;
select wg->'$[0].a' as wa from table;
3.5 column->>path
stay MySQL 5.7.13+, You can also use "->>" Indicates that the extraction result is removed " Number , The following three effects are the same :
- JSON_UNQUOTE( JSON_EXTRACT(column, path) )
- JSON_UNQUOTE(column -> path)
- column->>path
3.6 JSON_KEYS(json_doc[, path])
obtain json All the key values of the document in the specified path , Return to one json array

3.7 JSON_OVERLAPS(json_doc1, json_doc2)
Compare the two JSON Whether there are the same elements . That is, whether there is an intersection . With the return 1, No return 0
A partial match does not count as having the same element .
Two JSON When comparing objects , At least one of the two is the same name Of key And the same correspondence value, Then return to 1.
When comparing two scalars , Then compare whether the values are equal .
Scalar and array comparisons , Then judge whether the scalar is equal to an element of the array . Different data types are not equal .
MySQL 8.0.17 Added functions

3.8 JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] ...])
According to a string , Return to the target JSON Match in value Path expression for ,search_str and JSON Medium value Match when equal .
This function can query the sub element information .
The first parameter json_doc Is the target JSON.
The second parameter one_or_all You can choose one perhaps all.one Represents the path that returns a matching value .all Indicates that all paths are returned .
Parameters search_str Is the string to search . You can use %( Percent sign ) Represents any number of characters ,_( Underline ) Represents any character .
Parameters escape_char It's the escape character . The default is \. Write as an empty string or NULL when , It also defaults to \.
Parameters path Is a path expression , If it's written path, The matching result should be performed under the path expression .

3.9 value MEMBER OF(json_array)
Judge value Whether it is included in a JSON Array . Include returns 1, Otherwise return to 0.
If the data format is different, it will not be included .
value You can substitute other expressions for .
SELECT 17 MEMBER OF('[23, "abc", 17, "ab", 10]');
Four 、 change json Function of
JSON_ARRAY_APPEND(json_doc, path, val[, path, val] ...)
JSON_ARRAY_INSERT(json_doc, path, val[, path, val] ...)
5、 ... and 、 Inquire about json Function of property
JSON_DEPTH(json_doc)
JSON_LENGTH(json_doc[, path])
JSON_TYPE(json_val)
JSON_VALID(val)
quote :
MySQL :: MySQL 8.0 Reference Manual :: 11.5 The JSON Data Type
边栏推荐
- 18. `bs对象.节点名.next_sibling` previous_sibling 获取兄弟节点
- 0/1分数规划专题
- 「运维有小邓」日志分析工具使用越来越频繁的原因
- Flume配置2——监控之Ganglia
- Dynamics crm: among locally deployed servers, sandbox, unzip, VSS, asynchronous and monitor services
- What is a database? Database detailed notes! Take you into the database ~ you want to know everything here!
- [compilation principle] syntax analysis
- XSS vulnerability
- Liunx instruction
- Logical structure and physical structure
猜你喜欢

【Try to Hack】vulnhub narak

Chainsafe cross chain bridge deployment tutorial

Flume configuration 3 - interceptor filtering

Linux Installation mysql8

Liunx instruction

Hangfire details

一次 Keepalived 高可用的事故,让我重学了一遍它!

【摸鱼神器】UI库秒变低代码工具——表单篇(一)设计

Stm32cubemx learning (6) external interrupt experiment

How to solve the problem of insufficient memory space in Apple iPhone upgrade system?
随机推荐
Lock4j -- distributed lock Middleware -- customize the logic of lock acquisition failure
[compilation principle] syntax analysis
「运维有小邓」AD域委派
Following the crowd hurts you
一次 Keepalived 高可用的事故,让我重学了一遍它!
Application of twelve factors (4)
XSS漏洞
Nutch2.1 using eclipse debug to store the build process in MySQL on the windows platform
Koa 源码剖析
Flume configuration 1 - basic case
PowerShell command outputs only a list of directories
一个超赞的开源的图片去水印解决方案
「运维有小邓」审核并分析文件和文件夹访问权限
文件包含漏洞
Cmake开发-多目录工程
「运维有小邓」Active Directory 密码过期通知功能
注解
[compilation principle] type check
【编译原理】语义分析
Go: how to write a correct UDP server