博客
关于我
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 为某个字段的值加前缀、去掉前缀
查看>>
mysql 主从
查看>>
mysql 主从 lock_mysql 主从同步权限mysql 行锁的实现
查看>>
mysql 主从互备份_mysql互为主从实战设置详解及自动化备份(Centos7.2)
查看>>
mysql 主从关系切换
查看>>
MYSQL 主从同步文档的大坑
查看>>
mysql 主键重复则覆盖_数据库主键不能重复
查看>>
Mysql 事务知识点与优化建议
查看>>
Mysql 优化 or
查看>>
mysql 优化器 key_mysql – 选择*和查询优化器
查看>>
MySQL 优化:Explain 执行计划详解
查看>>
Mysql 会导致锁表的语法
查看>>
mysql 使用sql文件恢复数据库
查看>>
mysql 修改默认字符集为utf8
查看>>
Mysql 共享锁
查看>>
MySQL 内核深度优化
查看>>
mysql 内连接、自然连接、外连接的区别
查看>>
mysql 写入慢优化
查看>>
mysql 分组统计SQL语句
查看>>