MySQL multi-row functions


multiline function

insert image description here

Multi-line functions, that is, group functions, are also called aggregation functions. Their function is to perform statistical calculations on a group (at least 2) of records and obtain statistical results. When using multi-line functions, you need to pay attention to the following two points:

  • By default, multi-row functions ignore records with a value of null and do not take them into operation.
  • By default, multi-line functions will count duplicate values ​​and will not automatically remove duplicates.

Commonly used multi-line functions are introduced below, and input represents the field name, as shown in the following table:

group function name illustrate
AVG(input) average
SUM(input) to sum
MAX(input) find the maximum
MIN(input) Find the minimum
COUNT(input) total statistics

Next, demonstrate the above multi-line function.

When starting the following experiment, students are asked to import the world.sql downloaded in the previous experiment and switch databases.

source /home/project/world.sql

1. To calculate the average of the population of all countries in the country table, the SQL statement is implemented as follows:

MariaDB [world]> select avg(population) as 平均人口 from country;
+---------------+
| 平均人口      |
+---------------+
| 25434098.1172 |
+---------------+
1 row in set (0.001 sec)

Result analysis: The average value usually has decimal places, but it is inappropriate for data such as population to have decimal places. We can combine the one-line function for rounding in the previous chapter to remove the decimal point. The modified SQL statement is as follows:

MariaDB [world]> select round(avg(population), 0) as 平均人口 from country;
+--------------+
| 平均人口     |
+--------------+
|     25434098 |
+--------------+
1 row in set (0.000 sec)

Note: 0 decimal places are set in the round function, so the result only has the integer part.

2. To find the total population of all countries in the country table, the SQL statement is implemented as follows:

MariaDB [world]> select sum(population) as 人口总数 from country;
+--------------+
| 人口总数     |
+--------------+
|   6078749450 |
+--------------+
1 row in set (0.000 sec)

3. To find the population of the country with the largest population and the smallest population in the country table, the SQL statement is implemented as follows:

MariaDB [world]> select max(population) as 最多人口数, min(population) as 最少人口数 from country;
+-----------------+-----------------+
| 最多人口数      | 最少人口数      |
+-----------------+-----------------+
|      1277558000 |               0 |
+-----------------+-----------------+
1 row in set (0.000 sec)

Note: The data in the world sample database is only used for MySQL operation practice, and the authenticity of the data is not guaranteed.

4. To find the total number of countries in the country table, the SQL statement is implemented as follows:

MariaDB [world]> select count(name) as 国家总数 from country;
+--------------+
| 国家总数     |
+--------------+
|          239 |
+--------------+
1 row in set (0.000 sec)

Guess you like

Origin blog.csdn.net/m0_62617719/article/details/130843047