mysql中的常用聚集函数

MySQL聚集函数详解
本文介绍了MySQL中的常用聚集函数,包括sum()、max()、min()、avg()和count()等,并通过实例展示了如何使用这些函数进行数据聚合操作。

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)

这些聚集函数的参数也可以是表达式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值