MySQL 自学笔记之:数据库查询 DQL(Data Query Lanuage)汇总函数

本文详细介绍了SQL中的汇总函数,包括COUNT、SUM、AVG、MAX和MIN等,并通过实例展示了如何使用这些函数进行数据统计与分析。具体示例涵盖了员工薪资数据的计数、求和、平均值、最大值及最小值的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

> DQL (Data Query Lanuage)  汇总函数

COUNT, 用法:COUNT [ (*) | (DISTINCT | ALL) ] (COLUMN NAME)

SUM,用法:SUM ([DISTINCT] COLUMN NAME);

MAX,用法:....

MIN,用法:....

AVG,用法:....

练习:

mysql> select *
    -> from em_tbl
    -> where salary is not null;
+----+--------+------+----------+
| id | name   | age  | salary   |
+----+--------+------+----------+
|  1 | andrew |   25 | 10000.00 |
|  2 | ray    |   30 | 15000.00 |
|  3 | jerry  |   31 | 14000.00 |
|  4 | jane   |   21 |  9000.00 |
|  5 | mengo  |   40 | 15000.00 |
|  6 | lei    |   30 | 13000.00 |
|  9 | lin    | NULL |  9999.00 |
| 10 | wang   | NULL |  8888.00 |
+----+--------+------+----------+
8 rows in set (0.00 sec)

mysql>
mysql> select count(salary)
    -> from em_tbl
    -> where salary is not null;
+---------------+
| count(salary) |
+---------------+
|             8 |
+---------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> select count(distinct salary)
    -> from em_tbl
    -> where salary is not null;
+------------------------+
| count(distinct salary) |
+------------------------+
|                      7 |
+------------------------+
1 row in set (0.00 sec)

mysql>
mysql> select sum(salary)
    -> from em_tbl
    -> where salary is not null;
+-------------+
| sum(salary) |
+-------------+
|    94887.00 |
+-------------+
1 row in set (0.00 sec)

mysql>
mysql> select sum(distinct salary)
    -> from em_tbl
    -> where salary is not null;
+----------------------+
| sum(distinct salary) |
+----------------------+
|             79887.00 |
+----------------------+
1 row in set (0.00 sec)

mysql>
mysql> select avg(salary)
    -> from em_tbl
    -> where salary is not null;
+--------------+
| avg(salary)  |
+--------------+
| 11860.875000 |
+--------------+
1 row in set (0.00 sec)

mysql>
mysql> select max(salary)
    -> from em_tbl
    -> where salary is not null;
+-------------+
| max(salary) |
+-------------+
|    15000.00 |
+-------------+
1 row in set (0.00 sec)

mysql>
mysql> select min(salary)
    -> from em_tbl
    -> where salary is not null;
+-------------+
| min(salary) |
+-------------+
|     8888.00 |
+-------------+
1 row in set (0.00 sec)

mysql>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值