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

    你可能感兴趣的文章
    MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
    查看>>
    MySQL - ERROR 1406
    查看>>
    mysql - 视图
    查看>>
    MySQL - 解读MySQL事务与锁机制
    查看>>
    MTTR、MTBF、MTTF的大白话理解
    查看>>
    mt_rand
    查看>>
    mysql -存储过程
    查看>>
    mysql /*! 50100 ... */ 条件编译
    查看>>
    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
    查看>>