mysql中的json_extract怎么使用

发布时间:2023-04-18 17:06:47 作者:iii
来源:亿速云 阅读:94

这篇文章主要介绍“mysql中的json_extract怎么使用”,在日常操作中,相信很多人在mysql中的json_extract怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mysql中的json_extract怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、前言

mysql5.7版本开始支持JSON类型字段
json_extract可以完全简写为 ->
json_unquote(json_extract())可以完全简写为 ->>
下面介绍中大部分会利用简写

二、创建示例表

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"}]');
idcontent
1{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}
2[1, “apple”, “red”, {“age”: 18, “name”: “tom”}]

三、基本语法

- 获取JSON对象中某个key对应的value值

content:
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}

# 得到"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                |
+--------------------+

- 获取JSON数组中某个元素

content:
[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            |
+------------------+

- 获取JSON中的嵌套数据

结合前面介绍的两种获取方式,可以获取json数据中的嵌套数据

content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}
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                  |
+---------------------+

四、渐入佳境

- 获取JSON多个路径的数据

将会把多个路径的数据组合成数组返回

content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}

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", "湖南", "长沙"]                                              |
+----------------------------------------------------------------------+

- 路径表达式*的使用

将会把多个路径的数据组合成数组返回

# 先插入一条用于测试的数据
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”: “一年三班”}, “friend”: [{“age”: 20, “name”: “marry”}, {“age”: 21, “name”: “Bob”}], “address”: {“city”: “长沙”, “name”: “中央公园”}}

# 获取所有二级嵌套中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"]            |
+-----------------------------+

- 返回NULL值

content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}

寻找的JSON路径都不存在

# age路径不存在,返回NULL
# 若有多个路径,只要有一个路径存在则不会返回NULL
select json_extract(content,'$.price') from test_json where id = 1;
+---------------------------------+
| json_extract(content,'$.price') |
+---------------------------------+
| NULL                            |
+---------------------------------+

路径中有NULL

# 存在任意路径为NULL则返回NULL
select json_extract(content,'$.age',NULL) from test_json where id = 1;
+------------------------------------+
| json_extract(content,'$.age',NULL) |
+------------------------------------+
| NULL                               |
+------------------------------------+

- 返回错误

若第一个参数不是JSON类型的数据,则返回错误

select json_extract('{1,2]',$[0])

若路径表达式不规范,则返回错误

select content->'$age' from test_json where id = 1;
# 结果:
ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 1.

五、使用场景

JSON_EXTRACT函数通常用于要获取JSON中某个特定的数据或者要根据它作为判断条件时使用

到此,关于“mysql中的json_extract怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. windows 2003 iis+php5+mysql+phpmyadmin 安装配置介绍
  2. Windows XP上如何安装配置 Apache+PHP+Mysql+Zend

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql json_extract

上一篇:git报错OpenSSL SSL_read: Connection was reset, errno 10054怎么解决

下一篇:Redis分布式锁一定要避开的两个坑是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》