iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >mysql中json_extract的使用方法实例详解
  • 858
分享到

mysql中json_extract的使用方法实例详解

mysqljson_extractmysqljson_extract函数 2023-04-13 08:04:09 858人浏览 薄情痞子
摘要

目录一、前言二、创建示例表三、基本语法- 获取JSON对象中某个key对应的value值- 获取jsON数组中某个元素- 获取JSON中的嵌套数据四、渐入佳境- 获取JSON多个路径的数据- 路径表达式*的使用- 返回N

一、前言

Mysql5.7版本开始支持JSON类型字段,本文详细介绍json_extract函数如何获取mysql中的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值

  • json_extract函数中,第一个参数content表示json数据,第二个参数为json路径,其中$表示该json数据本身,$.name就表示获取json中key为name的value值
  • 可以利用 -> 表达式来代替json_extract
  • 若获取的val本身为字符串,那么获取的val会被引号包起来,比如"tom",这种数据被解析到程序对象中时,可能会被转义为\“tom\”。为了解决这个问题了,可以在外面再包上一层json_unquote函数,或者使用 ->> 代替->

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数组中某个元素

  • json_extract函数中,第一个参数content表示json数据,第二个参数为json路径,其中$表示该json数据本身,$[i]表示获取该json数组索引为i的元素(索引从0开始)
  • 与获取key-val一样,若获取的元素为字符串,默认的方式也会得到双引号包起来的字符,导致程序转义,方法也是利用json_unquote函数,或者使用 ->> 代替->

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中某个特定的数据或者要根据它作为判断条件时使用

六、参考文档

  • mysql5.7官方文档
  • https://www.sjkjc.com/mysql-ref/json_extract

总结

到此这篇关于mysql中json_extract使用方法的文章就介绍到这了,更多相关mysql中json_extract使用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

您可能感兴趣的文档:

--结束END--

本文标题: mysql中json_extract的使用方法实例详解

本文链接: https://www.lsjlt.com/news/201463.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql怎么查看表的索引
    通过查询系统表,可以获取表的索引信息,包括索引名称、是否唯一、索引类型、索引列和行数。常用系统表有:mysql 的 information_schema.statistics、postg...
    99+
    2024-05-14
    mysql oracle
  • sql怎么查看索引
    您可以使用 sql 通过以下方法查看索引:show indexes 语句:显示表中定义的索引列表及其信息。explain 语句:显示查询计划,其中包含用于执行查询的索引。informat...
    99+
    2024-05-14
  • sql怎么查看存储过程
    如何查看 sql 存储过程的源代码:使用 show create procedure 语句直接获取创建脚本。查询 information_schema.routines 表的 routi...
    99+
    2024-05-14
  • sql怎么查看视图表
    要查看视图表,可以使用以下步骤:使用 select 语句获取视图中的数据。使用 desc 语句查看视图的架构。使用 explain 语句分析视图的执行计划。使用 dbms 提供...
    99+
    2024-05-14
    oracle python
  • sql怎么查看创建的视图
    可以通过sql查询查看已创建的视图,具体步骤包括:连接到数据库并执行查询select * from information_schema.views;查询结果将显示视图的名称、...
    99+
    2024-05-14
    mysql
  • sql怎么用循环语句实现查询
    可以通过 do 和 while 语句创建循环,并在循环内执行查询,详细步骤包括:定义循环变量设置循环初始值循环执行查询更新循环变量执行查询循环退出条件 SQL 中使用循环语句实现查询 ...
    99+
    2024-05-14
  • sql怎么用代码修改表中数据
    通过 sql 代码修改表中数据的方法包括:修改单个记录:使用 update 语句设置列值并指定条件。修改多条记录:在 update 语句中指定多个条件来修改满足条件的所有记录。增加新列:...
    99+
    2024-05-14
  • sql怎么用命令创建数据库
    在 sql 中使用 create database 命令创建新数据库,其语法包含以下步骤:指定数据库名称。指定数据库文件和日志文件的位置(可选)。指定数据库大小、最大大小和文件增长(可选...
    99+
    2024-05-14
  • sql怎么用身份证提取年龄
    sql 中提取身份证号码中的年龄的方法:提取出生日期部分(身份证号码中第 7-14 位);使用 to_date 函数转换为日期格式;使用 extract 函数计算与当前日期之间的年差。 ...
    99+
    2024-05-14
  • sql怎么看字段长度
    有两种方法可查看 sql 中的字段长度:使用 information_schema 架构,其中包含元数据信息,可用于查询字段长度。使用内建函数,如 length(),其适用于字符串数据类...
    99+
    2024-05-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作