轻松上手,快乐学习!

MySQL 教程

MySQL 首页MySQL 介绍MySQL 安装MySQL 实例库下载MySQL 实例库导入MySQL SELECTMySQL DISTINCTMySQL ORDER BYMySQL WHEREMySQL ANDMySQL ORMySQL INMySQL BETWEENMySQL LIKEMySQL LIMITMySQL IS NULLMySQL 别名MySQL JOINSMySQL INNER JOINMySQL LEFT JOINMySQL RIGHT JOINMySQL CROSS JOINMySQL SELF JOINMySQL GROUP BYMySQL HAVINGMySQL ROLLUPMySQL 子查询MySQL 派生表MySQL EXISTSMySQL CTEMySQL 递归 CTEMySQL UNIONMySQL INTERSECTMySQL MINUSMySQL INSERTMySQL INSERT INTO SELECTMySQL INSERT IGNOREMySQL UPDATEMySQL UPDATE JOINMySQL DELETEMySQL ON DELETE CASCADEMySQL DELETE JOINMySQL REPLACEMySQL PREPAREMySQL 事务MySQL 表锁定MySQL USEMySQL 数据库管理MySQL CREATE DATABASEMySQL DROP DATABASEMySQL 存储引擎MySQL CREATE TABLEMySQL 序列MySQL ALTER TABLEMySQL RENAME TABLEMySQL DROP COLUMNMySQL ADD COLUMNMySQL DROP TABLEMySQL 临时表MySQL TRUNCATE TABLEMySQL 数据类型MySQL NOT NULLMySQL Primary KeyMySQL Foreign KeyMySQL UNIQUEMySQL CHECKMySQL 字符集MySQL 排序规则MySQL 导入 CSVMySQL 导出 CSVMySQL 自然排序MySQL 基础MySQL NOT INMySQL Storage Engines

MySQL 存储过程

MySQL 存储过程介绍MySQL 存储过程实例MySQL 存储过程变量MySQL 存储过程参数MySQL 存储过程返回多值MySQL IF 语句MySQL CASE 语句MySQL IF CASE 选择MySQL 存储过程循环MySQL 存储过程游标MySQL 存储过程列表MySQL 存储过程异常处理MySQL SIGNAL 和 RESIGNALMySQL 存储函数MySql 自定义存储

MySQL 视图

MySQL 视图教程SQL 视图介绍MySQL 视图介绍MySQL 创建视图MySQL 可更新视图WITH CHECK OPTIONLOCAL&CASCADEDMySQL 视图管理

MySQL 触发器

MySQL 触发器SQL 触发器MySQL 触发器介绍MySQL 触发器的创建MySQL 创建多个触发器MySQL 触发器管理MySQL 计划事件MySQL事件修改

MySQL 索引

MySQL 索引MySQL 索引创建MySQL 索引删除MySQL 显示索引MySQL 唯一索引MySQL 前缀索引MySQL 隐形索引MySQL 降序索引MySQL 复合索引MySQL 聚集索引MySQL 索引基数MySQL USE INDEXMySQL 强制索引

MySQL 管理

MySQL 管理MySQL访问控制系统MySQL 用户创建MySQL 用户密码MySQL 权限授予MySQL 权限撤销MySQL 角色MySQL 删除用户MySQL 表维护mysqldump 备份工具MySQL 数据库列表MySQL 表列表MySQL 表字段列表MySQL 用户列表MySQL 进程列表MySQL 列生成MySQL 表自连接MySQL 恢复

MySQL 全文搜索

MySQL 全文搜索MySQL 全文搜索介绍FULLTEXT索引MySQL 自然语言全文搜索MySQL 布尔全文搜索MySQL查询扩展MySQL ngram

MySQL 高级

MySQL 函数MySQL 窗口函数

MySQL 技巧

MySQL 查找重复数据MySQL 删除重复数据MySQL UUIDMySQL 表的复制MySQL 复制库MySQL 变量MySQL SELECT INTO 变量MySQL 表的存储引擎MySQL 使用正则查询MySQL 添加序号MySQL 随机查询MySQL 查询第 N 高记录MySQL 重置自增值MySQL VS MariaDBMySQL 间隔值MySQL 获取当天日期MySQL NULL 映射MySQL 注释理解MySQL EXPLAINMySQL 技巧介绍MySQL COUNTMySQL 数据分层MySQL 两表比较Mysql Like 优化

MySQL 窗口函数

MySQL CUME_DIST() 函数MySQL DENSE_RANK() 函数MySQL FIRST_VALUE 函数MySQL LAG() 函数MySQL LAST_VALUE() 函数MySQL LEAD 函数MySQL NTH_VALUE 函数MySQL NTILE 函数MySQL PERCENT_RANK 函数MySQL RANK 函数MySQL ROW_NUMBER 函数

MySQL 应用

MySQL应用程序编程接口PHP MySQL教程PHP 数据库连接Python 持久化实现

MySQL 优化

mysqld 内存持续变高求字符串交集


MySQL ROLLUP


简介:在本教程中,您将学习如何使用MySQL ROLLUP子句生成小计和总计。

设置样本表

以下语句创建一个的新表名为sales,用于存储按产品系列和年份汇总的订单值。数据来自于productsordersorderDetails中的表样本数据库
CREATE TABLE sales
SELECT
    productLine,
    YEAR(orderDate) orderYear,
    quantityOrdered * priceEach orderValue
FROM
    orderDetails
        INNER JOIN
    orders USING (orderNumber)
        INNER JOIN
    products USING (productCode)
GROUP BY
    productLine ,
    YEAR(orderDate);
以下查询返回表中的所有行sales
SELECT 
    *
FROM
    sales;
运行结果:
+------------------+-----------+------------+
| productLine      | orderYear | orderValue |
+------------------+-----------+------------+
| Classic Cars     |      2013 |    5571.80 |
| Classic Cars     |      2014 |    8124.98 |
| Classic Cars     |      2015 |    5971.35 |
| Motorcycles      |      2013 |    2440.50 |
| Motorcycles      |      2014 |    2598.77 |
| Motorcycles      |      2015 |    4004.88 |
| Planes           |      2013 |    4825.44 |
| Planes           |      2014 |    2857.35 |
| Planes           |      2015 |    4018.00 |
...

MySQL ROLLUP概述

分组集是要分组的一组列。例如,以下查询创建一个表示的分组集(productline)
SELECT 
    productline, 
    SUM(orderValue) totalOrderValue
FROM
    sales
GROUP BY 
    productline;
运行结果:
+------------------+-----------------+
| productline      | totalOrderValue |
+------------------+-----------------+
| Classic Cars     |        19668.13 |
| Motorcycles      |         9044.15 |
| Planes           |        11700.79 |
| Ships            |        13147.86 |
| Trains           |         9021.03 |
| Trucks and Buses |        14194.95 |
| Vintage Cars     |        12245.78 |
+------------------+-----------------+
7 rows in set (0.00 sec)
以下查询创建一个空分组集,表示为()
SELECT 
    SUM(orderValue) totalOrderValue
FROM
    sales;
运行结果:
+-----------------+
| totalOrderValue |
+-----------------+
|        89022.69 |
+-----------------+
1 row in set (0.00 sec)
如果要在一个查询中一起生成两个或多个分组集,使用UNION ALL按如下方式使用运算符:
SELECT 
    productline, 
    SUM(orderValue) totalOrderValue
FROM
    sales
GROUP BY 
    productline 
UNION ALL
SELECT 
    NULL, 
    SUM(orderValue) totalOrderValue
FROM
    sales;
这是查询输出:
+------------------+-----------------+
| productline      | totalOrderValue |
+------------------+-----------------+
| Classic Cars     |        19668.13 |
| Motorcycles      |         9044.15 |
| Planes           |        11700.79 |
| Ships            |        13147.86 |
| Trains           |         9021.03 |
| Trucks and Buses |        14194.95 |
| Vintage Cars     |        12245.78 |
| NULL             |        89022.69 |
+------------------+-----------------+
8 rows in set (0.01 sec)
因为UNION ALL要求所有查询具有相同数量的列,所以我们在第二个查询的选择列表中添加了NULL满足此要求的内容。 将NULLproductLine列中标识量线的总计。 此查询能够按产品线以及总计行生成总订单值。但是,它有两个问题:
  1. 查询非常冗长。
  2. 查询的性能可能不太好,因为数据库引擎必须在内部执行两个单独的查询并将结果集合并为一个。
要解决这些问题,可以使用ROLLUP子句。 ROLLUP子句是GROUP BY子句的扩展,具有以下语法:
SELECT 
    select_list
FROM 
    table_name
GROUP BY
    c1, c2, c3 WITH ROLLUP;
根据子句中ROLLUP指定的列或表达式生成多个分组集GROUP BY。 请参阅以下查询:
SELECT 
    productLine, 
    SUM(orderValue) totalOrderValue
FROM
    sales
GROUP BY 
    productline WITH ROLLUP;
输出结果:
+------------------+-----------------+
| productLine      | totalOrderValue |
+------------------+-----------------+
| Classic Cars     |        19668.13 |
| Motorcycles      |         9044.15 |
| Planes           |        11700.79 |
| Ships            |        13147.86 |
| Trains           |         9021.03 |
| Trucks and Buses |        14194.95 |
| Vintage Cars     |        12245.78 |
| NULL             |        89022.69 |
+------------------+-----------------+
8 rows in set (0.00 sec)
如输出中清楚地显示的那样,ROLLUP子句不仅生成小计,还生成订单值的总计。 如果在GROUP BY子句中指定了多个列,则ROLLUP子句假定输入列之间存在优先级结构。 例如:
GROUP BY c1, c2, c3 WITH ROLLUP
ROLLUP假设有以下优先级:
c1 > c2 > c3
它会生成以下分组集:
(c1, c2, c3)
(c1, c2)
(c1)
()
如果您在GROUP BY子句中指定了两列:
GROUP BY c1, c2 WITH ROLLUP
然后ROLLUP生成以下分组集:
(c1, c2)
(c1)
()
请参阅以下查询示例:
SELECT 
    productLine, 
    orderYear,
    SUM(orderValue) totalOrderValue
FROM
    sales
GROUP BY 
    productline, 
    orderYear 
WITH ROLLUP;
输出结果:
+------------------+-----------+-----------------+
| productLine      | orderYear | totalOrderValue |
+------------------+-----------+-----------------+
| Classic Cars     |      2013 |         5571.80 |
| Classic Cars     |      2014 |         8124.98 |
| Classic Cars     |      2015 |         5971.35 |
| Classic Cars     |      NULL |        19668.13 |
| Motorcycles      |      2013 |         2440.50 |
| Motorcycles      |      2014 |         2598.77 |
| Motorcycles      |      2015 |         4004.88 |
| Motorcycles      |      NULL |         9044.15 |
...
ROLLUP在每次产品线更改时生成小计行,在结果结束时生成总计。 在这种情况下的层次结构是:
productLine > orderYear
如果您反转层次结构,例如:
SELECT 
    orderYear,
    productLine, 
    SUM(orderValue) totalOrderValue
FROM
    sales
GROUP BY 
    orderYear,
    productline
WITH ROLLUP;
下图显示了输出:
+-----------+------------------+-----------------+
| orderYear | productLine      | totalOrderValue |
+-----------+------------------+-----------------+
|      2013 | Classic Cars     |         5571.80 |
|      2013 | Motorcycles      |         2440.50 |
|      2013 | Planes           |         4825.44 |
|      2013 | Ships            |         5072.71 |
|      2013 | Trains           |         2770.95 |
|      2013 | Trucks and Buses |         3284.28 |
|      2013 | Vintage Cars     |         4080.00 |
|      2013 | NULL             |        28045.68 |
|      2014 | Classic Cars     |         8124.98 |
|      2014 | Motorcycles      |         2598.77 |
|      2014 | Planes           |         2857.35 |
|      2014 | Ships            |         4301.15 |
|      2014 | Trains           |         4646.88 |
|      2014 | Trucks and Buses |         4615.64 |
|      2014 | Vintage Cars     |         2819.28 |
|      2014 | NULL             |        29964.05 |
|      2015 | Classic Cars     |         5971.35 |
|      2015 | Motorcycles      |         4004.88 |
|      2015 | Planes           |         4018.00 |
|      2015 | Ships            |         3774.00 |
|      2015 | Trains           |         1603.20 |
|      2015 | Trucks and Buses |         6295.03 |
|      2015 | Vintage Cars     |         5346.50 |
|      2015 | NULL             |        31012.96 |
|      NULL | NULL             |        89022.69 |
+-----------+------------------+-----------------+
25 rows in set (0.00 sec)
ROLLUP每年更改时生成小计,并在结果集的末尾生成总计。 此示例中的层级结构是:
orderYear > productLine

GROUPING() 函数

要检查NULL结果集中是否表示小计或总计,请使用GROUPING()函数。 当NULL在超级聚合时,GROUPING()函数返回1 ,否则返回0。 GROUPING()函数可用于查询,HAVING子句和(从MySQL 8.0.12开始)ORDER BY子句。 请考虑以下查询:
SELECT 
    orderYear,
    productLine, 
    SUM(orderValue) totalOrderValue,
    GROUPING(orderYear),
    GROUPING(productLine)
FROM
    sales
GROUP BY 
    orderYear,
    productline
WITH ROLLUP;
下图显示了输出:
+-----------+------------------+-----------------+---------------------+-----------------------+
| orderYear | productLine      | totalOrderValue | GROUPING(orderYear) | GROUPING(productLine) |
+-----------+------------------+-----------------+---------------------+-----------------------+
|      2013 | Classic Cars     |         5571.80 |                   0 |                     0 |
|      2013 | Motorcycles      |         2440.50 |                   0 |                     0 |
|      2013 | Planes           |         4825.44 |                   0 |                     0 |
|      2013 | Ships            |         5072.71 |                   0 |                     0 |
|      2013 | Trains           |         2770.95 |                   0 |                     0 |
|      2013 | Trucks and Buses |         3284.28 |                   0 |                     0 |
|      2013 | Vintage Cars     |         4080.00 |                   0 |                     0 |
|      2013 | NULL             |        28045.68 |                   0 |                     1 |
|      2014 | Classic Cars     |         8124.98 |                   0 |                     0 |
|      2014 | Motorcycles      |         2598.77 |                   0 |                     0 |
|      2014 | Planes           |         2857.35 |                   0 |                     0 |
|      2014 | Ships            |         4301.15 |                   0 |                     0 |
|      2014 | Trains           |         4646.88 |                   0 |                     0 |
|      2014 | Trucks and Buses |         4615.64 |                   0 |                     0 |
|      2014 | Vintage Cars     |         2819.28 |                   0 |                     0 |
|      2014 | NULL             |        29964.05 |                   0 |                     1 |
|      2015 | Classic Cars     |         5971.35 |                   0 |                     0 |
|      2015 | Motorcycles      |         4004.88 |                   0 |                     0 |
|      2015 | Planes           |         4018.00 |                   0 |                     0 |
|      2015 | Ships            |         3774.00 |                   0 |                     0 |
|      2015 | Trains           |         1603.20 |                   0 |                     0 |
|      2015 | Trucks and Buses |         6295.03 |                   0 |                     0 |
|      2015 | Vintage Cars     |         5346.50 |                   0 |                     0 |
|      2015 | NULL             |        31012.96 |                   0 |                     1 |
|      NULL | NULL             |        89022.69 |                   1 |                     1 |
+-----------+------------------+-----------------+---------------------+-----------------------+
25 rows in set (0.00 sec)
NULLorderYear 列发生在行聚合时, GROUPING(orderYear)时返回1 ,否则为0。 类似地,当NULLproductLine列发生行聚合时,GROUPING(productLine)当返回1 ,否则为0。 我们经常使用GROUPING()函数将有意义的标签替换为超级聚合NULL值,而不是直接显示它。 以下示例显示如何将IF()函数与GROUPING()函数组合以替换超聚合NULLorderYearproductLine列中的标签:
SELECT 
    IF(GROUPING(orderYear),
        'All Years',
        orderYear) orderYear,
    IF(GROUPING(productLine),
        'All Product Lines',
        productLine) productLine,
    SUM(orderValue) totalOrderValue
FROM
    sales
GROUP BY 
    orderYear , 
    productline 
WITH ROLLUP;
输出结果:
+-----------+-------------------+-----------------+
| orderYear | productLine       | totalOrderValue |
+-----------+-------------------+-----------------+
| 2013      | Classic Cars      |         5571.80 |
| 2013      | Motorcycles       |         2440.50 |
| 2013      | Planes            |         4825.44 |
| 2013      | Ships             |         5072.71 |
| 2013      | Trains            |         2770.95 |
| 2013      | Trucks and Buses  |         3284.28 |
| 2013      | Vintage Cars      |         4080.00 |
| 2013      | All Product Lines |        28045.68 |
| 2014      | Classic Cars      |         8124.98 |
| 2014      | Motorcycles       |         2598.77 |
| 2014      | Planes            |         2857.35 |
| 2014      | Ships             |         4301.15 |
| 2014      | Trains            |         4646.88 |
| 2014      | Trucks and Buses  |         4615.64 |
| 2014      | Vintage Cars      |         2819.28 |
| 2014      | All Product Lines |        29964.05 |
| 2015      | Classic Cars      |         5971.35 |
| 2015      | Motorcycles       |         4004.88 |
| 2015      | Planes            |         4018.00 |
| 2015      | Ships             |         3774.00 |
| 2015      | Trains            |         1603.20 |
| 2015      | Trucks and Buses  |         6295.03 |
| 2015      | Vintage Cars      |         5346.50 |
| 2015      | All Product Lines |        31012.96 |
| All Years | All Product Lines |        89022.69 |
+-----------+-------------------+-----------------+
25 rows in set, 2 warnings (0.00 sec)
在本教程中,您已经学习了如何使用MySQL ROLLUP()生成多个分组集,同时考虑GROUP BY子句中指定的列之间的层级结构。