Mysql的with rollup功能(5.1以上版本)

原文链接:http://jbm3072.iteye.com/blog/1168429


RollUp是上卷功能,类似于数据挖掘中的上卷操作。

ROLLUp的功能和Order by功能是互斥的。

mysql> SELECT year,SUM(profit) FROM sales GROUP BY year;
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 |        4525 |
| 2001 |        3010|
+------+-------------+

 

mysql> SELECT year,SUM(profit) FROM sales GROUP BY year WITH ROLLUP;
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 |        4525 |
| 2001 |        3010 |
| NULL |        7535 |
+------+-------------+

mysql> SELECT year,country, product, SUM(profit)
    ->
FROM sales
    ->
GROUP BY year, country, product;
+------+---------+------------+-------------+
| year | country | product    |SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer   |        1500 |
| 2000 | Finland | Phone      |         100 |
| 2000 | India   | Calculator |         150 |
| 2000 | India   | Computer   |       1200 |
| 2000 | USA     | Calculator |          75 |
| 2000 | USA     | Computer   |       1500 |
| 2001 | Finland | Phone      |          10 |
| 2001 | USA     | Calculator |          50 |
| 2001 | USA     | Computer   |       2700 |
| 2001 | USA     | TV         |         250|
+------+---------+------------+-------------+

 

mysql> SELECT year,country, product, SUM(profit)
    ->
FROM sales
    ->
GROUP BY year, country, product WITH ROLLUP;
+------+---------+------------+-------------+
| year | country | product    |SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer   |        1500 |
| 2000 | Finland | Phone      |         100 |
| 2000 |Finland | NULL       |        1600 |
| 2000 | India   | Calculator |         150 |
| 2000 | India   | Computer   |       1200 |
|
2000 |India   | NULL       |       1350 |
| 2000 | USA     | Calculator |          75 |
| 2000 | USA     | Computer   |       1500 |
| 2000 | USA     | NULL       |       1575 |
| 2000 | NULL    | NULL       |       4525
|
| 2001 | Finland | Phone      |          10 |
| 2001 | Finland | NULL       |          10 |
| 2001 | USA     | Calculator |          50 |
| 2001 | USA     | Computer   |       2700 |
| 2001 | USA     | TV         |         250 |
| 2001 | USA     | NULL       |       3000 |
| 2001 | NULL    | NULL       |       3010 |
| NULL | NULL    | NULL       |       7535 |

+------+---------+------------+-------------+

===============================================================

简单来说就是会自己加一个汇总。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值