前言
在现代数据库设计中,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"] | +-----------------------------------------+
json_object(key1, value1, key2, value2, ...)
创建一个 json 对象。key1, key2: json 对象的键;value1, value2: 与键相关联的值。
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 值
json_extract(json_doc, path[, type])
从 json 文档中选择的数据返回与 path 参数匹配的部分。
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" | +----+---------+
json_contains(target, candidate[, path])
函数用于检查一个 json 文档(target)是否包含另一个 json 文档或值(candidate),或者检查在指定的路径(path)下是否包含该候选者。它返回一个整数来表示结果:如果找到匹配项,则返回 1;否则返回 0。
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} | +----+----------------------------------------------------------------------------------+
json_search(json_doc, one_or_all, search_str[, escape_char[, path] ...])
函数返回 json 文档中指定字符串的路径。 json_doc,如果 search_str 或 path 参数为 、 path 在文档中不存在,或者 search_ 如果未找到 str,则返回 。
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 值
json_set(json_doc, path, val[, path, val] ...)
函数在 json 文档中插入或更新数据并返回结果。 如果任一参数是 或 path,则返回 (如果指定)。
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} | +----+----------------------------------------------------------------------------------+
json_replace(json_doc, path, val[, path, val] ...)
函数替换 json 文档中的现有值并返回结果。 如果任何参数为 ,则返回 。
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} | +----+----------------------------------------------------------------------------------+
json_remove(json_doc, path[, path] ...)
函数从 json 文档中删除数据并返回结果。path 参数从左到右计算。 通过评估一个路径生成的文档将成为评估下一个路径所依据的新值。
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 属性
json_keys(json_doc[, path])
函数以 json 数组的形式返回 json 对象的顶级值的键。 如果指定了 path 参数,则返回所选路径的顶级键。
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 表
json_table(expr, path columns (column_list) [as] alias)
从 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 函数
json_pretty(json_val)
格式化 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 } | +----+-----------------------------------------------------------------------------------------------+
json_length(json_doc[, path] ...)
返回 json 文档的长度。
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 | +----+--------+
- json_valid(val)
判断 json 值是否有效。返回 0 或 1 以指示该值是否为有效的 json。
mysql> select json_valid('hello'), json_valid('"hello"'); +---------------------+-----------------------+ | json_valid('hello') | json_valid('"hello"') | +---------------------+-----------------------+ | 0 | 1 | +---------------------+-----------------------+
注意事项
- 大小写敏感:大多数 json 函数对大小写敏感,因此在比较字符串时需要注意。
- 参数有效性:当输入参数为空或无效时,函数可能会返回 null。
- 性能优化:对于包含大量 json 数据的表,直接使用 json 函数可能导致性能下降。为了提高查询效率,可以考虑使用虚拟列(generated columns)结合索引来加速特定条件下的查询,或者将经常查询的字段提取到单独的列中进行索引。
总结
mysql 8.0 提供了丰富的 json 函数,使得处理 json 数据变得更加简单和高效。通过本文的详细介绍和实际应用示例,读者可以更好地理解和利用这些函数,在实际开发中发挥其最大价值。合理的设计和优化也是确保系统性能的关键因素之一。通过合理使用这些 json 函数,可以显著提升数据处理的灵活性和效率。希望本文能帮助读者在使用 mysql 8.0 处理 json 数据时更加得心应手。
到此这篇关于mysql中json 函数的具体使用的文章就介绍到这了,更多相关mysql json 函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论