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

> 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>


你可能感兴趣的:(SQL)