[MySQL Aggregate Query] Easily Realize Data Statistics

1. Aggregation query

Aggregate query: operations between rows

1.1 Aggregate functions

Common aggregate functions:

  • count: quantity

  • sum: Sum

  • avg: average value

  • max: maximum value

  • min: minimum value

1.2 Use of aggregate functions

1.2.1 count function

There is now a class table:

Now we use count to count how many classes are in the class table

-- select count(id) from class;
select count(*) from class;
+----------+
| count(*) |
+----------+
|        3 |
+----------+

1.2.2 sum function

Now there is a score table:

Now we use the sum function to find the sum of the language scores

select sum(language) from score;
+---------------+
| sum(language) |
+---------------+
|        283.50 |
+---------------+

1.2.3 avg function

Now there is a score table:

Now we use the avg function to find the average of the language scores

select avg(language) from score;
+---------------+
| avg(language) |
+---------------+
|     94.500000 |
+---------------+

1.2.4 max function

Now there is a score table:

Now we use the max function to find the maximum value of language scores

select max(language) from score;
+---------------+
| max(language) |
+---------------+
|         97.80 |
+---------------+

1.2.5 min function

Now there is a score table:

Now we use the min function to find the minimum value in the language score

select min(language) from score;
+---------------+
| min(language) |
+---------------+
|         89.50 |
+---------------+

1.3 group by child clause

1.3.1 Use of group by

group by can query the rows in the table by group

Without group by grouping, it is equivalent to only one group, and aggregate all rows

The introduction of group by can be aggregated separately for different groups

Now there is a salarytable salary table:

Query the average salary for each position:

select role,avg(salary) from salarytable group by role;
+--------+------------------+
| role   | avg(salary)      |
+--------+------------------+
| 服务员 |      6000.000000 |
| 程序猿 |     22500.000000 |
| 老板   | 110000000.000000 |
+--------+------------------+

Can group by be used for ordinary queries without aggregate functions?

Answer: Yes, but the result of the query is the first record of each group

select * from salarytable group by role;
+----+------+--------+--------------+
| id | name | role   | salary       |
+----+------+--------+--------------+
|  5 | 赵六 | 服务员 |      6000.00 |
|  3 | 张三 | 程序猿 |     20000.00 |
|  1 | 马云 | 老板   | 100000000.00 |
+----+------+--------+--------------+

1.3.2 Group query with specified conditions

Group query, you can specify conditions, usually use where and having to specify conditions

The difference between where and having:

  • Before grouping, specify the condition. Filter first, then group, use where

  • After grouping, specify conditions. Group first, then filter, using having

Note: In a SQL statement, conditions can be specified at the same time before and after grouping

1. Before grouping, specify the conditions

select role,avg(salary) from salarytable where role !='程序猿' group by role;
+--------+------------------+
| role   | avg(salary)      |
+--------+------------------+
| 服务员 |      6000.000000 |
| 老板   | 110000000.000000 |
+--------+------------------+

Drawing analysis:

2. After grouping, specify the conditions

select role,avg(salary) from salarytable group by role having role != '老板';
+--------+--------------+
| role   | avg(salary)  |
+--------+--------------+
| 服务员 |  6000.000000 |
| 程序猿 | 22500.000000 |
+--------+--------------+

Drawing analysis:

Guess you like

Origin blog.csdn.net/m0_66488562/article/details/129379419