MySQL table aggregate function

foreword

        Hello, everyone, this article introduces a few aggregate functions commonly used in MySQL. What is an aggregate function? I believe that friends who see this term for the first time are quite confused. For example, Speaking of the number of data in the statistical table, you can use the function COUNT provided in MySQL to make statistics. Let's take a look at what aggregate functions are available and how to use them.

1. Aggregate function

First, get a general understanding of what aggregation functions are available, and then learn each aggregation function in combination with specific cases:

case:

1. Count the number of students in the class

-- 使用 * 做统计,不受 NULL 影响

mysql> select* from student;
+----+--------+----+---------+
| id | name   | sn | qq      |
+----+--------+----+---------+
|  1 | 张三   |  1 | 123456  |
|  2 | 李四   |  2 | 1234578 |
|  3 | 王五   |  3 | 234567  |
|  5 | 张飞   |  6 | 2467764 |
|  6 | 刘备   |  4 | NULL    |
+----+--------+----+---------+
5 rows in set (0.00 sec)
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.01 sec)
-- 使用表达式做统计
mysql> select count(1) from student;
+----------+
| count(1) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

2. Count the number of QQ numbers collected by the class

-- NULL 不会计入结果
mysql> select count(qq) from student;
+-----------+
| count(qq) |
+-----------+
|         4 |
+-----------+
1 row in set (0.00 sec)

3. Count the number of math scores in this exam

mysql> select* from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  8 | 唐三藏    |      67 |   98 |      56 |
|  9 | 孙悟空    |      87 |   78 |      77 |
| 10 | 猪悟能    |      88 |   98 |      90 |
| 11 | 曹孟德    |      82 |   84 |      67 |
| 12 | 刘玄德    |      55 |   85 |      45 |
| 13 | 孙权      |      70 |   73 |      78 |
| 14 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
--count(math)统计的是全部的成绩
mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
|           7 |
+-------------+
1 row in set (0.00 sec)
--count(distinct math)统计的是去重的成绩
mysql> select count(distinct math) from exam_result;
+----------------------+
| count(distinct math) |
+----------------------+
|                    6 |
+----------------------+
1 row in set (0.00 sec)

4. Statistical math scores

mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
|       581 |
+-----------+
1 row in set (0.00 sec)

5. Statistical average score

mysql> select avg(math+english+chinese) 平均总分 from exam_result;
+--------------------+
| 平均总分           |
+--------------------+
| 221.14285714285714 |
+--------------------+
1 row in set (0.00 sec)

6. Return the highest score in English

mysql> select max(english) from exam_result;
+--------------+
| max(english) |
+--------------+
|           90 |
+--------------+
1 row in set (0.01 sec)

7. Return > 70 points above the minimum math score


mysql> select min(math) from exam_result where math > 70;
+-----------+
| min(math) |
+-----------+
|        73 |
+-----------+
1 row in set (0.00 sec)

2. Group statistics

The related use of aggregation functions is introduced above. Some friends may find it strange why group statistics suddenly appear here. Don’t worry, let’s understand what group statistics is. 

The purpose of grouping statistics is to realize the use of aggregation functions, so grouping is a means, and the ultimate goal is to realize aggregation statistics. It may be that it is not intuitive for everyone to understand. Let's illustrate through actual cases:

There are three tables here:

EMP employee table
DEPT department table
SALGRADE salary grade table

emp table:

 dept table:

salgrade table:

 

It can be observed from the first table that there are many employees' information in the table, each employee has different information, but several employees have the same information, which is the employee's department number:

At this time, there is a demand: display the average salary and final salary of each department

If you directly use the aggregation function for statistics, you will find that the above requirements cannot be achieved. At this time, you need to group first, and then use the corresponding aggregation function to count. This is the above-mentioned grouping is a means, and the real purpose is In order to achieve aggregate statistics.

Let's take a closer look at how to perform group statistics:

grammar:

select column1, column2, .. from table group by column;

case:

Display the average and maximum salaries for each department:

mysql> select deptno,max(sal) 最高,avg(sal) 平均 from emp group by deptno;
+--------+---------+-------------+
| deptno | 最高    | 平均        |
+--------+---------+-------------+
|     10 | 5000.00 | 2916.666667 |
|     20 | 3000.00 | 2175.000000 |
|     30 | 2850.00 | 1566.666667 |
+--------+---------+-------------+
3 rows in set (0.00 sec)

Show the average and minimum wages for each job in each sector:

mysql> select deptno,job,avg(sal) 平均,min(sal) 最低 from emp group by deptno,job;
+--------+-----------+-------------+---------+
| deptno | job       | 平均        | 最低    |
+--------+-----------+-------------+---------+
|     10 | CLERK     | 1300.000000 | 1300.00 |
|     10 | MANAGER   | 2450.000000 | 2450.00 |
|     10 | PRESIDENT | 5000.000000 | 5000.00 |
|     20 | ANALYST   | 3000.000000 | 3000.00 |
|     20 | CLERK     |  950.000000 |  800.00 |
|     20 | MANAGER   | 2975.000000 | 2975.00 |
|     30 | CLERK     |  950.000000 |  950.00 |
|     30 | MANAGER   | 2850.000000 | 2850.00 |
|     30 | SALESMAN  | 1400.000000 | 1250.00 |
+--------+-----------+-------------+---------+
9 rows in set (0.00 sec)

Display the departments with average salary less than 2000 and their average salary:

Implementation ideas:

a. Calculate the average salary of each department:

mysql> select deptno,avg(sal) 平均工资 from emp group by deptno;
+--------+--------------+
| deptno | 平均工资     |
+--------+--------------+
|     10 |  2916.666667 |
|     20 |  2175.000000 |
|     30 |  1566.666667 |
+--------+--------------+
3 rows in set (0.00 sec)

b. Filter the results:

Having and group by are used together to filter the results of group by

mysql> select deptno,avg(sal) 平均工资 from emp group by deptno having 平均工资 < 2000;
+--------+--------------+
| deptno | 平均工资     |
+--------+--------------+
|     30 |  1566.666667 |
+--------+--------------+
1 row in set (0.00 sec)

--having is often used in conjunction with group by, the function is to filter the group, the function is like where.

Summarize

        This article introduces what aggregate functions are, how to use aggregate functions, and group statistics. After understanding these usage rules, it will be more convenient and faster to operate MySQL table data in the future.

Guess you like

Origin blog.csdn.net/qq_65307907/article/details/131487652