从mysql中提取数据,使用ROLLUP统计后,想在数据左边列中加入项目名称,使用SQL:
复制代码 代码示例:
SELECT (CASE WHEN ISNULL(PLAYERNO) THEN PLAYERNO='TOTAL' ELSE PLAYERNO END) AS PLAYERNO,SUM(A
linuxjishu/9952.html target=_blank class=infotextkey>mount) FROM penalties
GROUP BY PLAYERNO WITH ROLLUP
输出结果:
复制代码 代码示例:
+----------+-------------+
| PLAYERNO | SUM(AMOUNT) |
+----------+-------------+
| 6 | 100.00 |
| 8 | 25.00 |
| 27 | 175.00 |
| 44 | 130.00 |
| 104 | 50.00 |
| NULL| 480.00 |
+----------+-------------+
6 rows in set, 1 warning (0.00 sec)
显示为空值,修改如下:
复制代码 代码示例:
SELECT PLAYERNO,SUM(AMOUNT) FROM penalties
GROUP BY PLAYERNO
UNION
SELECT 'TOTAL',SUM(AMOUNT)
FROM penalties
显示结果:
复制代码 代码示例:
+----------+-------------+
| PLAYERNO | SUM(AMOUNT) |
+----------+-------------+
| 6 | 100.00 |
| 8 | 25.00 |
| 27 | 175.00 |
| 44 | 130.00 |
| 104 | 50.00 |
| TOTAL | 480.00 |
+----------+-------------+
6 rows in set (0.00 sec)
问题:虽然这样也可以显示出数据,但却没有了ROLLUP的简洁,哪位有更好的方法,欢迎分享下。