我有一个mysql查询(由sagi创建): -
SELECT 'blr' AS Location, COUNT(*) AS playCnt, (select count(*) from blr_skip WHERE blr_skip_dt like '%2016-03-20%') as skipCnt FROM blr_play WHERE blr_play_dt like '%2016-03-20%' UNION SELECT 'chen' AS Location, COUNT(*) AS playCnt, (select count(*) from chen_skip WHERE chen_skip_dt like '%2016-03-20%') as skipCnt FROM chen_play WHERE chen_play_dt like '%2016-03-20%';
给出如下输出: -
+----------+---------+---------+
| Location | playCnt | skipCnt |
+----------+---------+---------+
| blr | 25 | 8 |
| chen | 12 | 1 |
+----------+---------+---------+
我想在列的末尾添加一行总计,如下所示: -
+----------+---------+---------+
| Location | playCnt | skipCnt |
+----------+---------+---------+
| blr | 25 | 8 |
| chen | 12 | 1 |
+----------+---------+---------+
|Total | 37 | 9 |
+----------+---------+---------+
答案 0 :(得分:2)
试试这个:
SELECT
IFNULL(Location, 'Total') AS Location,
SUM(playCnt) AS playCnt,
SUM(skipCnt) AS playCnt
FROM
(
SELECT 'blr' AS Location, COUNT(*) AS playCnt, (select count(*) from blr_skip WHERE blr_skip_dt like '%2016-03-20%') as skipCnt
FROM blr_play
WHERE blr_play_dt like '%2016-03-20%'
UNION
SELECT 'chen' AS Location, COUNT(*) AS playCnt, (select count(*) from chen_skip WHERE chen_skip_dt like '%2016-03-20%') as skipCnt
FROM chen_play
WHERE chen_play_dt like '%2016-03-20%'
) t1
GROUP BY Location WITH ROLLUP;