mysql5.7 version begins to support JSON type fields2. Create Example tablejson_extract can be completely abbreviated as
-> json_unquote(json_extract()) can be completely abbreviated as
->>Most of the following introductions will use the abbreviation
CREATE TABLE `test_json` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` json DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
# 插入两条测试用的记录
INSERT INTO `test_json` (`content`) VALUES ('{\"name\":\"tom\",\"age\":18,\"score\":[100,90,87],\"address\":{\"province\":\"湖南\",\"city\":\"长沙\"}}');
INSERT INTO `test_json` (`content`) VALUES ('[1, "apple", "red", {"age": 18, "name": "tom"}]');
content | |
---|---|
{"age": 18, "name": "tom", "score": [100, 90, 87], "address": {"city": "Changsha", "province": "Hunan"}} | |
[1, “apple”, “red”, {“age”: 18, “name”: “tom”}] |
- In the json_extract function, the first One parameter content represents json data, and the second parameter is the json path, where $ represents the json data itself, and $.name represents obtaining the value with the key name in json
- You can use -> expression instead of json_extract
- If the obtained val itself is a string, then the obtained val will be wrapped in quotation marks, such as "tom", this kind of data is When parsed into a program object, it may be escaped as \"tom\". In order to solve this problem, you can wrap another layer of json_unquote function outside, or use ->> instead of ->
content:- Get an element in the JSON array{" age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “Changsha”, “province”: “Hunan”}}
# 得到"tom" select json_extract(content,'$.name') from test_json where id = 1; # 简写方式:字段名->表达式等价于json_extract(字段名,表达式) select content->'$.name' from test_json where id = 1; # 结果: +--------------------------------+ | json_extract(content,'$.name') | +--------------------------------+ | "tom" | +--------------------------------+ +-------------------+ | content->'$.name' | +-------------------+ | "tom" | +-------------------+ # 解除双引号,得到tom select json_unquote(json_extract(content,'$.name')) from test_json where id = 1; # 简写方式:字段名->>表达式等价于json_unquote(json_extract(字段名,表达式)) select content->>'$.name' from test_json where id = 1; # 结果: +----------------------------------------------+ | json_unquote(json_extract(content,'$.name')) | +----------------------------------------------+ | tom | +----------------------------------------------+ +--------------------+ | content->>'$.name' | +--------------------+ | tom | +--------------------+
- In the json_extract function, the first parameter content represents json data, and the second parameter is the json path, where $ Represents the json data itself, $[i] indicates obtaining the element with index i of the json array (the index starts from 0)
- is the same as obtaining key-val, if the obtained element is For strings, the default method will also get characters enclosed in double quotes, causing the program to escape. The method is also to use the json_unquote function, or use ->> instead of ->
[1, “apple”, “red”, {“age”: 18, “name”: “tom”}]
# 得到"apple" select json_extract(content,'$[1]') from test_json where id = 2; # 简写,效果同上 select content->'$[1]' from test_json where id = 2; # 结果: +------------------------------+ | json_extract(content,'$[1]') | +------------------------------+ | "apple" | +------------------------------+ +-----------------+ | content->'$[1]' | +-----------------+ | "apple" | +-----------------+ # 解除双引号,得到apple select json_unquote(json_extract(content,'$[1]')) from test_json where id = 2; # 简写,效果同上 select content->>'$[1]' from test_json where id = 2; # 结果: +--------------------------------------------+ | json_unquote(json_extract(content,'$[1]')) | +--------------------------------------------+ | apple | +--------------------------------------------+ +------------------+ | content->>'$[1]' | +------------------+ | apple | +------------------+- Get JSON Nested data
Combined with the two acquisition methods introduced earlier, you can obtain the nested data in json data
content: id=1{"age": 18 , “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “Changsha”, “province”: “Hunan”}}content: id= 2
[1, “apple”, “red”, {“age”: 18, “name”: “tom”}]# 得到:87 select content->'$.score[2]' from test_json where id = 1; # 结果: +-----------------------+ | content->'$.score[2]' | +-----------------------+ | 87 | +-----------------------+ # 得到:18 select content->'$[3].age' from test_json where id = 2; # 结果: +---------------------+ | content->'$[3].age' | +---------------------+ | 18 | +---------------------+4. Getting better
- Obtain JSON data of multiple paths
Will combine the data of multiple paths into an array and return
content: id=1{"age": 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “Changsha”, “province”: “Hunan”}}
select json_extract(content,'$.age','$.score') from test_json where id = 1; # 结果: +-----------------------------------------+ | json_extract(content,'$.age','$.score') | +-----------------------------------------+ | [18, [100, 90, 87]] | +-----------------------------------------+ select json_extract(content,'$.name','$.address.province','$.address.city') from test_json where id = 1; # 结果: +----------------------------------------------------------------------+ | json_extract(content,'$.name','$.address.province','$.address.city') | +----------------------------------------------------------------------+ | ["tom", "湖南", "长沙"] | +----------------------------------------------------------------------+- The use of path expression *
will combine the data of multiple paths into an array and return
# 先插入一条用于测试的数据 INSERT INTO `test_json` (`id`,`content`) VALUES(3,'{"name":"tom","address":{"name":"中央公园","city":"长沙"},"class":{"id":3,"name":"一年三班"},"friend":[{"age":20,"name":"marry"},{"age":21,"name":"Bob"}]}')content: id=3
{“name ": "tom", "class": {"id": 3, "name": "Class Three a Year"}, "friend": [{"age": 20, "name": "marry"}, {"age": 21, "name": "Bob"}], "address": {"city": "Changsha", "name": "Central Park"}}content: id=1
# 获取所有二级嵌套中key=name的值 # 由于friend的二级嵌套是一个数组,所以.name获取不到其中的所有name值 select content->'$.*.name' from test_json where id = 3; +----------------------------------+ | content->'$.*.name' | +----------------------------------+ | ["一年三班", "中央公园"] | +----------------------------------+``` # 获取所有key为name值的数据,包括任何嵌套内的name select content->'$**.name' from test_json where id = 3; +---------------------------------------------------------+ | content->'$**.name' | +---------------------------------------------------------+ | ["tom", "一年三班", "marry", "Bob", "中央公园"] | +---------------------------------------------------------+ # 获取数组中所有的name值 select content->'$.friend[*].name' from test_json where id = 3; +-----------------------------+ | content->'$.friend[*].name' | +-----------------------------+ | ["marry", "Bob"] | +-----------------------------+- Return NULL value
{"age": 18, "name": "tom", "score": [100, 90, 87], "address" ": {"city": "Changsha", "province": "Hunan"}}The JSON path you are looking for does not exist
# age路径不存在,返回NULL # 若有多个路径,只要有一个路径存在则不会返回NULL select json_extract(content,'$.price') from test_json where id = 1; +---------------------------------+ | json_extract(content,'$.price') | +---------------------------------+ | NULL | +---------------------------------+
There are NULL
in the path# 存在任意路径为NULL则返回NULL select json_extract(content,'$.age',NULL) from test_json where id = 1; +------------------------------------+ | json_extract(content,'$.age',NULL) | +------------------------------------+ | NULL | +------------------------------------+
- Return error
If the first parameter is not JSON type data, return error
select json_extract('{1,2]',$[0])
If the path expression is not standardized, return error
select content->'$age' from test_json where id = 1; # 结果: ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 1.
5. Usage scenarios
The JSON_EXTRACT function is usually used when you want to obtain a specific data in JSON or use it as a judgment condition
The above is the detailed content of How to use json_extract in mysql. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
