本文共 3415 字,大约阅读时间需要 11 分钟。
以下是用于测试 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_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;
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"] |
select json_extract(content, '$.price') from test_json where id = 1;
结果:
| json_extract(content, '$.price') |
|---|
| 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_EXTRACT,可以高效地从 JSON 数据中提取所需信息,满足复杂的数据处理需求。
转载地址:http://gsbfk.baihongyu.com/