1. 程式人生 > >分組group by

分組group by

where VS having

  • WHERE過濾行,而HAVING過濾分組
  • WHERE後不能使用聚合函式過濾行,而HAVING可以使用聚合函式過濾分組
  • WHERE在資料分組前進行過濾,HAVING在資料分組後進行過濾

分組彙總 WITH ROLLUP

select class,ssex,count(sname) from student group by class,ssex;
+-------+------+--------------+
| class | ssex | count(sname) |
+-------+------+--------------+
| 95031 | 女   |            1 |
| 95031 | 男   |            2 |
| 95033 | 女   |            1 |
| 95033 | 男   |            2 |
+-------+------+--------------+
select class,ssex,count(sname) from student group by class,ssex with rollup;
+-------+------+--------------+
| class | ssex | count(sname) |
+-------+------+--------------+
| 95031 | 女   |            1 |
| 95031 | 男   |            2 |
| 95031 | NULL |            3 |
| 95033 | 女   |            1 |
| 95033 | 男   |            2 |
| 95033 | NULL |            3 |
| NULL  | NULL |            6 |
+-------+------+--------------+