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)
————————————————
或者你可以使用 ROLLUP, 它能用一个问询提供双层分析。将一个 WITH ROLLUP修饰符添加到GROUP BY 语句,使询问产生另一行结果,该行显示了所有年份的总价值:mysql>SELECT year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 |4525 |
| 2001 |3010 |
| NULL |7535 |
+------+-------------+
总计高聚集行被年份列中的NULL值标出。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)