This article mainly introduces several basic tutorials on data Statistics Query in MySQL, including statistic result query methods such as average and maximum and minimum values.
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:
The code is as follows:
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
The above is the basic usage tutorial _ MySQL for several data statistics queries in MySQL. For more information, see The PHP Chinese website (www.php1.cn )!