JSON Functions That Aggregate JSON Values
本页面列出的函数是 TiDB 支持的 aggregate functions 的一部分,但专门用于处理 JSON。
JSON_ARRAYAGG()
JSON_ARRAYAGG(key)
函数根据给定的 key
将值聚合成一个 JSON 数组。key
通常是一个表达式或列名。
示例:
这里将表中一列的两个行聚合成一个 JSON 数组。
SELECT JSON_ARRAYAGG(v) FROM (SELECT 1 'v' UNION SELECT 2);
+------------------+
| JSON_ARRAYAGG(v) |
+------------------+
| [2, 1] |
+------------------+
1 行结果(0.00 秒)
JSON_OBJECTAGG()
JSON_OBJECTAGG(key,value)
函数根据给定的 key
和 value
将键值对聚合成一个 JSON 对象。key
和 value
通常是表达式或列名。
示例:
首先,创建两个表并插入一些行。
CREATE TABLE plants (
id INT PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE plant_attributes (
id INT PRIMARY KEY AUTO_INCREMENT,
plant_id INT, attribute VARCHAR(255),
value VARCHAR(255),
FOREIGN KEY (plant_id) REFERENCES plants(id)
);
INSERT INTO plants
VALUES
(1,"rose"),
(2,"tulip"),
(3,"orchid");
INSERT INTO plant_attributes(plant_id,attribute,value)
VALUES
(1,"color","red"),
(1,"thorns","yes"),
(2,"color","orange"),
(2,"thorns","no"),
(2,"grows_from","bulb"),
(3,"color","white"),
(3, "thorns","no");
然后可以查看创建的表内容。
TABLE plants;
+----+--------+
| id | name |
+----+--------+
| 1 | rose |
| 2 | tulip |
| 3 | orchid |
+----+--------+
3 行结果(0.00 秒)
TABLE plant_attributes;
+----+----------+------------+--------+
| id | plant_id | attribute | value |
+----+----------+------------+--------+
| 1 | 1 | color | red |
| 2 | 1 | thorns | yes |
| 3 | 2 | color | orange |
| 4 | 2 | thorns | no |
| 5 | 2 | grows_from | bulb |
| 6 | 3 | color | white |
| 7 | 3 | thorns | no |
+----+----------+------------+--------+
7 行结果(0.00 秒)
你可以使用 JSON_OBJECTAGG()
函数结合这些数据。这里可以看到,对于每个分组,多个键/值对被聚合成一个 JSON 对象。
SELECT
p.name,
JSON_OBJECTAGG(attribute,value)
FROM
plant_attributes pa
LEFT JOIN plants p ON pa.plant_id=p.id
GROUP BY
plant_id;
+--------+-----------------------------------------------------------+
| name | JSON_OBJECTAGG(attribute,value) |
+--------+-----------------------------------------------------------+
| rose | {"color": "red", "thorns": "yes"} |
| orchid | {"color": "white", "thorns": "no"} |
| tulip | {"color": "orange", "grows_from": "bulb", "thorns": "no"} |
+--------+-----------------------------------------------------------+
3 行结果(0.00 秒)