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

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

MySQL 中 JSON_EXTRACT 的使用方法

创建示例表

以下是用于测试 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"}]');

表内容如下:

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

基本语法

获取 JSON 中某个键对应的值

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

示例

select json_extract(content, '$.name') from test_json where id = 1;

简化语法:

select content -> '$.name' from test_json where id = 1;

结果

json_extract(content, '$.name')
"tom"

简化语法结果:

content -> '$.name'
"tom"

如果需要解除双引号,可以使用 JSON_UNQUOTE

select json_unquote(json_extract(content, '$.name')) from test_json where id = 1;

简化语法:

select content -> '->$..name' from test_json where id = 1;

获取 JSON 中某个元素

示例

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"

如果需要解除双引号:

select json_unquote(json_extract(content, '$[1]')) from test_json where id = 2;

简化语法:

select content -> '->$[1]' from test_json where id = 2;

获取嵌套数据

示例

select content -> '$.score[2]' from test_json where id = 1;

结果:

content -> '$.score[2]'
87

另一个示例:

select content -> '$[3].age' from test_json where id = 2;

结果:

content -> '$[3].age'
18
---## 渐入佳境### 获取多个路径数据#### 示例```sqlselect 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"}]}');

获取嵌套对象中的所有名称

select content -> '$.*.name' from test_json where id = 3;

结果:

content -> '$.*.name'
["一年三班", "中央公园"]

获取所有名称,包括嵌套对象内的名称

select content -> '$**.name' from test_json where id = 3;

结果:

content -> '$**.name'
["tom", "一年三班", "marry", "Bob", "中央公园"]

获取嵌套数组中的所有名称

select content -> '$.friend[*].name' from test_json where id = 3;

结果:

content -> '$.friend[*].name'
["marry", "Bob"]

返回 NULL 值

JSON 路径不存在的情况

select json_extract(content, '$.price') from test_json where id = 1;

结果:

json_extract(content, '$.price')
NULL

路径中存在 NULL

select json_extract(content, '$.age', NULL) from test_json where id = 1;

结果:

json_extract(content, '$.age', NULL)
NULL
---## 返回错误### 参数不正确```sqlselect json_extract('{1,2]', '[0]') from test_json;

错误信息:

ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 1.

路径表达式不规范

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 数据中提取特定键对应的值;
  • 从嵌套或数组数据中提取所需信息;
  • 用于条件判断或计算。
  • 通过合理使用 JSON_EXTRACT,可以高效地从 JSON 数据中提取所需信息,满足复杂的数据处理需求。

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

    你可能感兴趣的文章