博客
关于我
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/

你可能感兴趣的文章
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>