mysql中的常用聚集函数如下:
示例中使用的数据表内容如下:
MariaDB [test]> select * from stu;
+------+----------+-------+
| id | name | class |
+------+----------+-------+
| 1 | xiaoming | 2 |
| 1 | liwen | 2 |
| 3 | xiaobai | 1 |
+------+----------+-------+
3 rows in set (0.00 sec)
sum()函数
用来返回集合中的所有值的和
MariaDB [test]> select sum(id) from stu;
+---------+
| sum(id) |
+---------+
| 5 |
+---------+
1 row in set (0.00 sec)
max()函数
返回集合中的最大值
MariaDB [test]> select max(class) from stu;
+------------+
| max(class) |
+------------+
| 2 |
+------------+
1 row in set (0.01 sec)
min()函数
返回集合中的最小值
MariaDB [test]> select min(id) from stu;
+---------+
| min(id) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
avg()函数
返回集合中所有数据的平均值
MariaDB [test]> select avg(id) from stu;
+---------+
| avg(id) |
+---------+
| 1.6667 |
+---------+
1 row in set (0.00 sec)
count()函数
返回集合中值的个数
MariaDB [test]> select count(class) from stu;
+--------------+
| count(class) |
+--------------+
| 3 |
+--------------+
1 row in set (0.00 sec)
MariaDB [test]> select count(*) from stu;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
这些聚集函数的参数也可以是表达式
本文介绍了MySQL中的常用聚集函数,包括sum()、max()、min()、avg()和count()等,并通过实例展示了如何使用这些函数进行数据聚合操作。
704

被折叠的 条评论
为什么被折叠?



