mysql>SELECT year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 |4525 |
| 2001 |3010 |
| NULL |7535 |
+------+-------------+
总计高聚集行被年份列中的NULL值标出。
当用select*from 表名 group by ‘字段名1’ 将选出来的内容将按照字段1分组,其他列不尽相同,会以最前面的内容显示:
GROUP BY 后面可以跟多个列名,表示以多列作为分组依据:
mysql>select `name`,`date`,count(*) from `employee_tbl` group by `name`,date
+------+---------------------+----------+
| name | date | count(*) |
+------+---------------------+----------+
| 小丽 | 2016-04-19 15:26:02 | 1 |
| 小明 | 2016-04-04 15:26:54 | 1 |
| 小明 | 2016-04-11 15:26:40 | 1 |
| 小明 | 2016-04-22 15:25:33 | 1 |
| 小王 | 2016-04-07 15:26:14 | 1 |
| 小王 | 2016-04-20 15:25:47 | 1 |
+------+---------------------+----------+
6 rows in set (0.05 sec)
group by 后可以跟with rollup,表示在进行分组统计的基础上再次进行汇总统计(在每个分组下都会有统计汇总):
mysql>select*from r
+------+-----+------+
| name | cat | fish |
+------+-----+------+
| 小明 | x | 1 |
| 小明 | x | 2 |
| 小明 | x | 4 |
| 小明 | y | 2 |
| 小明 | y | 2 |
| 小明 | z | 1 |
| 小红 | x | 1 |
| 小红 | x | 3 |
| 小红 | y | 1 |
| 小红 | y | 2 |
| 小青 | y | 3 |
| 小青 | z | 2 |
| 小青 | z | 1 |
+------+-----+------+
13 rows in set (0.05 sec)
mysql>select name,cat,fish,count(*) from r group by name with rollup
+------+-----+------+----------+
| name | cat | fish | count(*) |
+------+-----+------+----------+
| 小红 | x | 1 | 4 |
| 小明 | x | 1 | 6 |
| 小青 | y | 3 | 3 |
| NULL | y | 3 | 13 |
+------+-----+------+----------+
4 rows in set (0.05 sec)
mysql>select name,cat,fish,count(*) from r group by name,cat with rollup
+------+------+------+----------+
| name | cat | fish | count(*) |
+------+------+------+----------+
| 小红 | x | 1 | 2 |
| 小红 | y | 1 | 2 |
| 小红 | NULL | 1 | 4 |
| 小明 | x | 1 | 3 |
| 小明 | y | 2 | 2 |
| 小明 | z | 1 | 1 |
| 小明 | NULL | 1 | 6 |
| 小青 | y | 3 | 1 |
| 小青 | z | 2 | 2 |
| 小青 | NULL | 2 | 3 |
| NULL | NULL | 2 | 13 |
+------+------+------+----------+
11 rows in set (0.06 sec)
mysql>select name,cat,fish,count(*) from r group by name,cat,fish with rollup
+------+------+------+----------+
| name | cat | fish | count(*) |
+------+------+------+----------+
| 小红 | x | 1 | 1 |
| 小红 | x | 3 | 1 |
| 小红 | x | NULL | 2 |
| 小红 | y | 1 | 1 |
| 小红 | y | 2 | 1 |
| 小红 | y | NULL | 2 |
| 小红 | NULL | NULL | 4 |
| 小明 | x | 1 | 1 |
| 小明 | x | 2 | 1 |
| 小明 | x | 4 | 1 |
| 小明 | x | NULL | 3 |
| 小明 | y | 2 | 2 |
| 小明 | y | NULL | 2 |
| 小明 | z | 1 | 1 |
| 小明 | z | NULL | 1 |
| 小明 | NULL | NULL | 6 |
| 小青 | y | 3 | 1 |
| 小青 | y | NULL | 1 |
| 小青 | z | 1 | 1 |
| 小青 | z | 2 | 1 |
| 小青 | z | NULL | 2 |
| 小青 | NULL | NULL | 3 |
| NULL | NULL | NULL | 13 |
+------+------+------+----------+
23 rows in set (0.07 sec)
mysql>select name,cat,fish,sum(fish) from r group by name with rollup
+------+-----+------+-----------+
| name | cat | fish | sum(fish) |
+------+-----+------+-----------+
| 小红 | x | 1 | 7 |
| 小明 | x | 1 | 12 |
| 小青 | y | 3 | 6 |
| NULL | y | 3 | 25 |
+------+-----+------+-----------+
4 rows in set (0.05 sec)
mysql>select name,cat,fish,sum(fish) from r group by name,cat with rollup
+------+------+------+-----------+
| name | cat | fish | sum(fish) |
+------+------+------+-----------+
| 小红 | x | 1 | 4 |
| 小红 | y | 1 | 3 |
| 小红 | NULL | 1 | 7 |
| 小明 | x | 1 | 7 |
| 小明 | y | 2 | 4 |
| 小明 | z | 1 | 1 |
| 小明 | NULL | 1 | 12 |
| 小青 | y | 3 | 3 |
| 小青 | z | 2 | 3 |
| 小青 | NULL | 2 | 6 |
| NULL | NULL | 2 | 25 |
+------+------+------+-----------+
11 rows in set (0.06 sec)
mysql>select name,cat,fish,sum(fish) from r group by name,cat,fish with rollup
+------+------+------+-----------+
| name | cat | fish | sum(fish) |
+------+------+------+-----------+
| 小红 | x | 1 | 1 |
| 小红 | x | 3 | 3 |
| 小红 | x | NULL | 4 |
| 小红 | y | 1 | 1 |
| 小红 | y | 2 | 2 |
| 小红 | y | NULL | 3 |
| 小红 | NULL | NULL | 7 |
| 小明 | x | 1 | 1 |
| 小明 | x | 2 | 2 |
| 小明 | x | 4 | 4 |
| 小明 | x | NULL | 7 |
| 小明 | y | 2 | 4 |
| 小明 | y | NULL | 4 |
| 小明 | z | 1 | 1 |
| 小明 | z | NULL | 1 |
| 小明 | NULL | NULL | 12 |
| 小青 | y | 3 | 3 |
| 小青 | y | NULL | 3 |
| 小青 | z | 1 | 1 |
| 小青 | z | 2 | 2 |
| 小青 | z | NULL | 3 |
| 小青 | NULL | NULL | 6 |
| NULL | NULL | NULL | 25 |
+------+------+------+-----------+
23 rows in set (0.07 sec)
————————————————
1、如果只是要【分别统计出每个用户每种结果的次数】,那只需要
select name,result,count(1) countfrom info
group by name,result
这个应该是知道的。
2、但要求是【0也要有单独的一行记录】,那相当于要填充几条0的结果,可以通过join的笛卡尔积连接,实现所有可能,↓
select * from (select DISTINCT `name` from info)t1,(select DISTINCT result from info)t23、此时已经快接近结果了,只需将上面两个结果集进行左连接,再分组即可↓
select m.*,count(n.result) count from(select * from (select DISTINCT `name` from info)t1,(select DISTINCT result from info)t2)m
left join
info n on m.`name`=n.`name` and m.result=n.result
group by m.`name`,m.result
4、由于需要【增加每个用户的总 *** 作数】,只需要在最后面加上with rollup即可,mysql里的一个关键字,专门用来分组统计,↓
select m.*,count(n.result) count from(select * from (select DISTINCT `name` from info)t1,(select DISTINCT result from info)t2)m
left join
info n on m.`name`=n.`name` and m.result=n.result
group by m.`name`,m.result
with rollup
看到没,很接近结果了。
5、整理优化下,把最后一条总的统计过滤掉,总 *** 作数加进去
select t.`name`,ifnull(t.result,'总 *** 作') result,t.countfrom (select m.*,count(n.result) count
from (select * from (select DISTINCT `name` from info)t1,(select DISTINCT result from info)t2)m
left join
info n on m.`name`=n.`name` and m.result=n.result
group by m.`name`,m.result
with rollup
)t
where t.name is not null
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)