MySQL分组查询

根据性别进行学生表的分组
SELECT  COUNT(*) FROM stu WHERE gender IS NOT NULL GROUP BY gender;
+----------+
| COUNT(*) |
+----------+
|        6 |
|        4 |
+----------+

SELECT gender, COUNT(*) FROM stu WHERE gender IS NOT NULL GROUP BY gender;
mysql> SELECT gender, COUNT(*) FROM stu WHERE gender IS NOT NULL GROUP BY gender;
+--------+----------+
| gender | COUNT(*) |
+--------+----------+
| male   |        6 |
| female |        4 |
+--------+----------+

查询每个不猛编号和部门的工资和
mysql> SELECT deptno , SUM(sal) FROM employee GROUP BY deptno;
+--------+----------+
| deptno | SUM(sal) |
+--------+----------+
|     20 | 10875.00 |
|     30 |  9400.00 |
|     10 |  3750.00 |
+--------+----------+
mysql> 

你可能感兴趣的:(MySQL分组查询)