Basic tutorial on statistical query of several data types in MySQL,

Source: Internet
Author: User

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 (*)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.