博客
关于我
mysql中json_extract的使用方法
阅读量:790 次
发布时间:2023-02-11

本文共 2958 字,大约阅读时间需要 9 分钟。

MySQL 中 JSON_EXTRACT 的使用方法

前言

MySQL 5.7 版本后开始支持 JSON 类型字段,本文将详细介绍 JSON_EXTRACT 函数的使用方法,帮助您获取 MySQL 中的 JSON 数据。

JSON_EXTRACT 函数可以通过简写形式进行调用,具体如下:

  • json_extract 可以完全简写为 ->
  • json_unquote(json_extract()) 可以完全简写为 ->>

本文将基于这些简写形式进行介绍。


创建示例表

为了演示 JSON_EXTRACT 的使用,我们先创建一个示例表 test_json,字段 content 为 JSON 类型。

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"}]');

表结构如下:

id content
1 {"age": 18, "name": "tom", "score": [100, 90, 87], "address": {"province": "湖南", "city": "长沙"}}
2 [1, "apple", "red", {"age": 18, "name": "tom"}]

基本语法

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

JSON_EXTRACT 函数的第一个参数是 JSON 数据,第二个参数是 JSON 路径。

  • 路径 $.name 表示获取 JSON 数据中 key 为 "name" 的 value。
  • 路径 $[i] 表示获取 JSON 数组中索引为 i 的元素。

示例:

-- 获取 "tom"select json_extract(content, '$.name') from test_json where id = 1;-- 简写方式select content -> '$.name' from test_json where id = 1;

如果 value 本身是字符串,会被引号包裹,导致转义。为了去除引号,可以使用 json_unquote

-- 解除引号,得到 "tom"select json_unquote(json_extract(content, '$.name')) from test_json where id = 1;-- 简写方式select content ->> '$.name' from test_json where id = 1;

获取 JSON 中某个元素

如果需要获取 JSON 数组中的某个元素,可以使用索引。

-- 获取 "apple"select json_extract(content, '$[1]') from test_json where id = 2;-- 简写方式select content -> '$[1]' from test_json where id = 2;

同样,数组中的元素如果是字符串,会被引号包裹,使用 json_unquote 可以去除引号。

-- 解除引号,得到 "apple"select json_unquote(json_extract(content, '$[1]')) from test_json where id = 2;-- 简写方式select content ->> '$[1]' from test_json where id = 2;

获取嵌套数据

JSON_EXTRACT 可以处理嵌套的 JSON 数据。

  • 获取嵌套对象中的 "score" 值。
  • 获取嵌套对象中的 "age" 值。

示例:

-- 获取 87select content -> '$.score[2]' from test_json where id = 1;-- 获取 18select content -> '$[3].age' from test_json where id = 2;

渐入佳境

获取多个路径的数据

JSON_EXTRACT 可以返回多个路径的数据组合成数组。

示例:

-- 获取 age 和 scoreselect json_extract(content, '$.age', '$.score') from test_json where id = 1;-- 结果:[18, [100, 90, 87]]-- 获取 name、地址省市select json_extract(content, '$.name', '$.address.province', '$.address.city') from test_json where id = 1;-- 结果:["tom", "湖南", "长沙"]

路径表达式 * 的使用

* 可以表示匹配所有符合条件的键。

示例:

-- 获取所有 key 为 "name" 的值select content -> '$*.name' from test_json where id = 3;-- 结果:["一年三班", "中央公园"]-- 获取所有嵌套中的 name 值select content -> '$**.name' from test_json where id = 3;-- 结果:["tom", "一年三班", "marry", "Bob", "中央公园"]-- 获取朋友数组中的 nameselect content -> '$.friend[*].name' from test_json where id = 3;-- 结果:["marry", "Bob"]

返回 NULL 值

如果 JSON 路径不存在,JSON_EXTRACT 会返回 NULL。

示例:

-- 寻找不存在的路径select json_extract(content, '$.price') from test_json where id = 1;-- 结果:NULL

使用场景

JSON_EXTRACT 函数适用于以下场景:

  • 提取 JSON 数据中的特定字段。
  • 处理嵌套 JSON 数据。
  • 执行复杂的 JSON 路径操作。

通过合理运用 JSON_EXTRACT,您可以高效地提取和处理 JSON 数据,满足开发需求。

转载地址:http://wpbfk.baihongyu.com/

你可能感兴趣的文章
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>