Basic tutorial on statistical query of several data types in MySQL,
Average statistics
The select avg () FROM syntax is used to calculate the average statistics FROM a data table.
Syntax:
SELECT AVG(column) FROM tb_name
This SQL syntax is used to calculate the average number of fields of a numerical type. AVG () cannot contain multiple fields. Strings and other types can be executed, but they are meaningless.
Example:
SELECT AVG(uid) FROM user
Query Result:
2.5000
Of course, counting the average uid here is meaningless, just to demonstrate the usage of the AVG () syntax.
Sum of statistical data
The select sum () FROM syntax is used to SUM the statistics FROM a data table.
Syntax:
SELECT SUM(column) FROM tb_name
This SQL syntax is used to calculate the SUM of values of a numeric field. SUM () cannot contain multiple fields. Strings and other types can be executed, but they are meaningless.
Example:
SELECT SUM(uid) FROM user
Query Result:
Copy codeCode: 10
Maximum statistics
The select max () FROM syntax is used to count the maximum data of a field FROM a data table.
Syntax:
SELECT MAX(column) FROM tb_name
This SQL syntax is used to calculate the maximum value of a field of a numerical type. The MAX () cannot contain multiple fields.
Example:
SELECT MAX(uid) FROM user
Query Result:
4
Minimum statistical data
The select min () FROM syntax is used to count the minimum data of a field FROM a data table.
Syntax:
SELECT MIN(column) FROM tb_name
For more information, see MAX ().
Description
The preceding statistical queries include common field queries that can be used in combination:
SELECT MAX(uid) as max,MIN(uid)as min,AVG(uid) as avg FROM user
The query result is as follows:
max min avg4 1 2.5000
However, when querying statistics and common fields, the results are not expected. For example, to query the largest uid user name (including uid ):
// This method is incorrect. Although select max (uid) can be executed, username FROM user // This method is correct: SELECT uid, username FROM user order by uid desc limit 1
Articles you may be interested in:
- MySQL query statistics per select statement
- MySQL statistics query implementation code
- In MYSQL, the use of the IF function (case) for SUM field statistics by condition
- The convenient way to count the total number of rows in the query result in MYSQL saves count (*)