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)
這些聚集函數的參數也可以是表達式