it编程 > 编程语言 > Javascript

MySQL中JSON 函数的具体使用

13人参与 2025-07-24 Javascript

前言

在现代数据库设计中,json 格式的数据因其灵活性和可扩展性而变得越来越受欢迎。mysql 8.0 引入了许多强大的 json 函数,使得处理 json 数据变得更加方便和高效。本文将通过一个简化的订单表 orders,展示如何使用这些 json 函数来创建、搜索、修改和验证 json 数据,从而优化订单管理系统。

1. 表结构定义

1.1 创建订单表 orders

首先,我们定义一个简单的订单表 orders,其中包含一个主键 id 和一个存储订单详细信息的 json 字段 data

create table orders (
    id int auto_increment primary key,
    data json
);

1.2 插入示例数据

接下来,我们插入一些示例数据,模拟不同水果的订单信息。

insert into orders (data) values
  ('{"fruit": "apple", "quantity": 100, "price": 25.0, "labels": ["fresh", "sweet"]}'),
  ('{"fruit": "banana", "quantity": 150, "price": 8.0, "labels": ["ripe"]}'),
  ('{"fruit": "cherry", "quantity": 120, "price": 15.0, "labels": ["small"]}'),
  ('{"fruit": "apple", "quantity": 50, "price": 12.5, "labels": ["fresh", "sweet"]}');

1.3 查询全部数据

mysql> select * from orders;
+----+----------------------------------------------------------------------------------+
| id | data                                                                             |
+----+----------------------------------------------------------------------------------+
|  1 | {"fruit": "apple", "price": 25.0, "labels": ["fresh", "sweet"], "quantity": 100} |
|  2 | {"fruit": "banana", "price": 8.0, "labels": ["ripe"], "quantity": 150}           |
|  3 | {"fruit": "cherry", "price": 15.0, "labels": ["small"], "quantity": 120}         |
|  4 | {"fruit": "apple", "price": 12.5, "labels": ["fresh", "sweet"], "quantity": 50}  |
+----+----------------------------------------------------------------------------------+

2. 使用 json 函数

2.1 创建 json 值

json_array(val1, val2, ...)
创建一个 json 数组。val1, val2, ... : 要包含在 json 数组中的值。

mysql> select json_array('apple', 'banana', 'cherry');
+-----------------------------------------+
| json_array('apple', 'banana', 'cherry') |
+-----------------------------------------+
| ["apple", "banana", "cherry"]           |
+-----------------------------------------+
mysql> select json_object('fruit', 'apple', 'quantity', 100, 'price', 25.0, 'labels', json_array('fresh', 'sweet'));
+-------------------------------------------------------------------------------------------------------+
| json_object('fruit', 'apple', 'quantity', 100, 'price', 25.0, 'labels', json_array('fresh', 'sweet')) |
+-------------------------------------------------------------------------------------------------------+
| {"fruit": "apple", "price": 25.0, "labels": ["fresh", "sweet"], "quantity": 100}                      |
+-------------------------------------------------------------------------------------------------------+

2.2 搜索 json 值

mysql> select id, json_extract(data, '$.price') as price, json_extract(data, '$.labels') as labels from orders order by price desc;
+----+-------+--------------------+
| id | price | labels             |
+----+-------+--------------------+
|  1 | 25.0  | ["fresh", "sweet"] |
|  3 | 15.0  | ["small"]          |
|  4 | 12.5  | ["fresh", "sweet"] |
|  2 | 8.0   | ["ripe"]           |
+----+-------+--------------------+


mysql> select id, json_extract(data, '$.labels[0]') as label from orders;
+----+---------+
| id | label   |
+----+---------+
|  1 | "fresh" |
|  2 | "ripe"  |
|  3 | "small" |
|  4 | "fresh" |
+----+---------+
mysql> select id, data from orders where json_contains(data, '"apple"', '$.fruit');
+----+----------------------------------------------------------------------------------+
| id | data                                                                             |
+----+----------------------------------------------------------------------------------+
|  1 | {"fruit": "apple", "price": 25.0, "labels": ["fresh", "sweet"], "quantity": 100} |
|  4 | {"fruit": "apple", "price": 12.5, "labels": ["fresh", "sweet"], "quantity": 50}  |
+----+----------------------------------------------------------------------------------+
mysql> select id, json_search(data, 'one','%e%', 'a') from orders;
+----+-------------------------------------+
| id | json_search(data, 'one','%e%', 'a') |
+----+-------------------------------------+
|  1 | "$.fruit"                           |
|  2 | "$.labels[0]"                       |
|  3 | "$.fruit"                           |
|  4 | "$.fruit"                           |
+----+-------------------------------------+

mysql> select id, json_search(data,'all' ,'%e%') from orders;
+----+-------------------------------------------+
| id | json_search(data,'all' ,'%e%')            |
+----+-------------------------------------------+
|  1 | ["$.fruit", "$.labels[0]", "$.labels[1]"] |
|  2 | "$.labels[0]"                             |
|  3 | "$.fruit"                                 |
|  4 | ["$.fruit", "$.labels[0]", "$.labels[1]"] |
+----+-------------------------------------------+

2.3 修改 json 值

mysql> update orders set data = json_set(data, '$.price', 26.0) where id = 1;

mysql> select * from orders where id = 1;
+----+----------------------------------------------------------------------------------+
| id | data                                                                             |
+----+----------------------------------------------------------------------------------+
|  1 | {"fruit": "apple", "price": 26.0, "labels": ["fresh", "sweet"], "quantity": 100} |
+----+----------------------------------------------------------------------------------+
mysql> update orders set data = json_replace(data, '$.quantity', 110) where id = 1;

mysql> select * from orders where id = 1;
+----+----------------------------------------------------------------------------------+
| id | data                                                                             |
+----+----------------------------------------------------------------------------------+
|  1 | {"fruit": "apple", "price": 26.0, "labels": ["fresh", "sweet"], "quantity": 110} |
+----+----------------------------------------------------------------------------------+

mysql> update orders set data = json_replace(data, '$.labels[0]', 'crisp‌') where id = 1;

mysql> select * from orders where id = 1;
+----+----------------------------------------------------------------------------------+
| id | data                                                                             |
+----+----------------------------------------------------------------------------------+
|  1 | {"fruit": "apple", "price": 26.0, "labels": ["crisp‌", "sweet"], "quantity": 110} |
+----+----------------------------------------------------------------------------------+
mysql> update orders set data = json_remove(data, '$.quantity') where id = 1;

mysql> select * from orders;
+----+---------------------------------------------------------------------------------+
| id | data                                                                            |
+----+---------------------------------------------------------------------------------+
|  1 | {"fruit": "apple", "price": 26.0, "labels": ["crisp‌", "sweet"]}                 |
|  2 | {"fruit": "banana", "price": 8.0, "labels": ["ripe"], "quantity": 150}          |
|  3 | {"fruit": "cherry", "price": 15.0, "labels": ["small"], "quantity": 120}        |
|  4 | {"fruit": "apple", "price": 12.5, "labels": ["fresh", "sweet"], "quantity": 50} |
+----+---------------------------------------------------------------------------------+

2.4 返回 json 属性

mysql> select id, json_keys(data) as `keys` from orders;
+----+------------------------------------------+
| id | keys                                     |
+----+------------------------------------------+
|  1 | ["fruit", "price", "labels"]             |
|  2 | ["fruit", "price", "labels", "quantity"] |
|  3 | ["fruit", "price", "labels", "quantity"] |
|  4 | ["fruit", "price", "labels", "quantity"] |
+----+------------------------------------------+

2.5 生成 json 表

mysql> select o.id, jt.fruit, jt.quantity, jt.price, jt.comments
from orders o,
json_table(
    o.data,
    '$' columns(
        fruit varchar(255) path '$.fruit',
        quantity int path '$.quantity',
        price decimal(10, 2) path '$.price',
        comments json path '$.comments'
    )
) as jt;
+----+--------+----------+-------+----------+
| id | fruit  | quantity | price | comments |
+----+--------+----------+-------+----------+
|  1 | apple  | null     | 26.00 | null     |
|  2 | banana |      150 | 8.00  | null     |
|  3 | cherry |      120 | 15.00 | null     |
|  4 | apple  |       50 | 12.50 | null     |
+----+--------+----------+-------+----------+

2.6 其他 json 函数

mysql> select id, json_pretty(data) from orders;
+----+-----------------------------------------------------------------------------------------------+
| id | json_pretty(data)                                                                             |
+----+-----------------------------------------------------------------------------------------------+
|  1 | {
  "fruit": "apple",
  "price": 26.0,
  "labels": [
    "crisp‌",
    "sweet"
  ]
}                  |
|  2 | {
  "fruit": "banana",
  "price": 8.0,
  "labels": [
    "ripe"
  ],
  "quantity": 150
}             |
|  3 | {
  "fruit": "cherry",
  "price": 15.0,
  "labels": [
    "small"
  ],
  "quantity": 120
}           |
|  4 | {
  "fruit": "apple",
  "price": 12.5,
  "labels": [
    "fresh",
    "sweet"
  ],
  "quantity": 50
} |
+----+-----------------------------------------------------------------------------------------------+
mysql> select id, json_length(data) as length from orders;
+----+--------+
| id | length |
+----+--------+
|  1 |      3 |
|  2 |      4 |
|  3 |      4 |
|  4 |      4 |
+----+--------+

mysql> select id, json_length(data, '$.labels') as length from orders;
+----+--------+
| id | length |
+----+--------+
|  1 |      2 |
|  2 |      1 |
|  3 |      1 |
|  4 |      2 |
+----+--------+
mysql> select json_valid('hello'), json_valid('"hello"');
+---------------------+-----------------------+
| json_valid('hello') | json_valid('"hello"') |
+---------------------+-----------------------+
|                   0 |                     1 |
+---------------------+-----------------------+

注意事项

总结

mysql 8.0 提供了丰富的 json 函数,使得处理 json 数据变得更加简单和高效。通过本文的详细介绍和实际应用示例,读者可以更好地理解和利用这些函数,在实际开发中发挥其最大价值。合理的设计和优化也是确保系统性能的关键因素之一。通过合理使用这些 json 函数,可以显著提升数据处理的灵活性和效率。希望本文能帮助读者在使用 mysql 8.0 处理 json 数据时更加得心应手。

到此这篇关于mysql中json 函数的具体使用的文章就介绍到这了,更多相关mysql json 函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

Nginx优化和模块解读

07-24

详解MySQL中JSON数据类型用法及与传统JSON字符串对比

07-23

C#解析JSON数据全攻略指南

07-22

telnet命令检查端口fs方式

07-20

JVM之jstack命令的使用解读

07-20

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

07-19

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论