差点。
?
MariaDB [hellodb]> SELECT COUNT(Name) FROM students;
+-------------+
| COUNT(Name) |
+-------------+
| ? ? ? ? ?25 |
+-------------+
1 row in set (0.00 sec)
?
我们也可以不让它显示上面的字段名称,给它换一个名称。?
?
3、AS ?别名。
?
MariaDB [hellodb]> SELECT COUNT(Name) AS CC FROM students;
+----+
| CC |
+----+
| 25 |
+----+
1 row in set (0.00 sec)
还有表也可以有别名,在多表查询的时候再来说说。
?
下面男同学的最小年龄,并用别名显示。
?
MariaDB [hellodb]> SELECT MIN(Age) AS Min_M FROM students WHERE gender = 'M';
+-------+
| Min_M |
+-------+
| ? ?19 |
+-------+
1 row in set (0.00 sec)
可不可以一次性男女分开显示各自的最小年龄。那就要用分组了。可以按性别gender来分组。这样函数就会分别计算各组的数据。
?
4、分组。
?
GROUP BY 字段名
?
以字段的值分组。同一个值一个组。然后再通过用聚合函数来统计不同组中的信息。
?
现在以gender分组,也就是两组。函数分别计算两个组。
不过下面这个有点缺陷,不知道哪是女的,哪个是男的。
?
MariaDB [hellodb]> SELECT MIN(Age) FROM students GROUP BY gender;
+----------+
| MIN(Age) |
+----------+
| ? ? ? 17 |
| ? ? ? 19 |
+----------+
2 rows in set (0.00 sec)
?
MariaDB [hellodb]>
?
下面再显示出来性别字段。
?
MariaDB [hellodb]> SELECT MIN(Age),gender FROM students GROUP BY gender;
+----------+--------+
| MIN(Age) | gender |
+----------+--------+
| ? ? ? 17 | F ? ? ?|
| ? ? ? 19 | M ? ? ?|
+----------+--------+
2 rows in set (0.00 sec)
?
MariaDB [hellodb]>
?
例:显示不同班级的学生个数
?
MariaDB [hellodb]> SELECT Count(Name),ClassID FROM students GROUP BY classID;
+-------------+---------+
| Count(Name) | ClassID |
+-------------+---------+
| ? ? ? ? ? 2 | ? ?NULL |
| ? ? ? ? ? 4 | ? ? ? 1 |
| ? ? ? ? ? 3 | ? ? ? 2 |
| ? ? ? ? ? 4 | ? ? ? 3 |
| ? ? ? ? ? 4 | ? ? ? 4 |
| ? ? ? ? ? 1 | ? ? ? 5 |
| ? ? ? ? ? 4 | ? ? ? 6 |
| ? ? ? ? ? 3 | ? ? ? 7 |
+-------------+---------+
8 rows in set (0.00 sec)
?
MariaDB [hellodb]>
?
不显示没有班级的。WHERE在分组之前先进行过滤,然后把数据再给GROUP BY来进行分组。
?
MariaDB [hellodb]> SELECT Count(Name),ClassID FROM students WHERE ClassID IS NOT NULL GROUP BY classID;
+-------------+---------+
| Count(Name) | ClassID |
+-------------+---------+
| ? ? ? ? ? 4 | ? ? ? 1 |
| ? ? ? ? ? 3 | ? ? ? 2 |
| ? ? ? ? ? 4 | ? ? ? 3 |
| ? ? ? ? ? 4 | ? ? ? 4 |
| ? ? ? ? ? 1 | ? ? ? 5 |
| ? ? ? ? ? 4 | ? ? ? 6 |
| ? ? ? ? ? 3 | ? ? ? 7 |
+-------------+---------+
7 rows in set (0.00 sec)
?
MariaDB [hellodb]>
?
例9:各个班级的平均年龄。
?
MariaDB [hellodb]> SELECT AVG(age),ClassID FROM students GROUP BY ClassID;
+----------+---------+
| AVG(age) | ClassID |
+----------+---------+
| ?63.5000 | ? ?NULL |
| ?20.5000 | ? ? ? 1 |
| ?36.0000 | ? ? ? 2 |
| ?20.2500 | ? ? ? 3 |
| ?24.7500 | ? ? ? 4 |
| ?46.0000 | ? ? ? 5 |
| ?20.7500 | ? ? ? 6 |
| ?19.6667 | ? ? ? 7 |
+----------+---------+
8 rows in set (0.00 sec)
?
MariaDB [hellodb]>
?
加上排序呢:
?
MariaDB [hellodb]> SELECT AVG(age),ClassID FROM students GROUP BY ClassID ORDER BY AVG(age);
+----------+---------+
| AVG(age) | ClassID |
+----------+---------+
| ?19.6667 | ? ? ? 7 |
| ?20.2500 | ? ? ? 3 |
| ?20.5000 | ? ? ? 1 |
| ?20.7500 | ? ? ? 6 |
| ?24.7500 | ? ? ? 4 |
| ?36.0000 | ? ? ? 2 |
| ?46.0000 | ? ? ? 5 |
| ?63.5000 | ? ?NULL |
+----------+---------+
8 rows in set (0.00 sec)
?
意思就是在分组之后,把各个分组重新排序了。以各个组的age字段的平均值来排序。
?
回来看分组:如果不想显示平均年龄小于等于25的,怎么办呢。
?
5、分组过滤。
?
HAVING 用于对分组做条件过滤。
?
普及:WHERE是对表中的每一行做过滤,单位是行。 而HAVING是对每一个组做过滤,单位是组。
如:
?
MariaDB [hellodb]> SELECT AVG(age),ClassID FROM students GROUP BY ClassID HAVING AVG(age)>25;
+----------+-