Basic usage of several statistical queries in MySQL _ MySQL

Source: Internet
Author: User
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 )!

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.