Basic use of several data statistics queries in Mysql _mysql

Source: Internet
Author: User

Statistical average
the SELECT AVG () from syntax is used to average statistics from data tables.
Grammar:

SELECT AVG (column) from Tb_name

This SQL syntax is used to count the average of a numeric type field, and the AVG () cannot be more than one field, and the type of string can be executed but meaningless.
Example:

SELECT AVG (UID) from user 

Get the result of the query:

2.5000 

Of course, the average of this statistic uid is meaningless, just to demonstrate the usage of AVG () syntax.

The sum of statistical data
the SELECT SUM () from syntax is used to sum the data from the data table.
Grammar:

SELECT SUM (column) from Tb_name

This SQL syntax is used to count the sum of the values of a numeric Type field, and the SUM () cannot be more than one field, while a type such as a string can be executed but meaningless.
Example:

SELECT SUM (UID) from user 

Get the result of the query:

Copy Code code as follows:
10

Statistics Maximum data
the SELECT max () from syntax is used to count a field's maximum data from a datasheet.
Grammar:

SELECT MAX (column) from Tb_name

This SQL syntax is used to count the maximum value of a numeric Type field and cannot be more than one field in MAX ().
Example:

SELECT MAX (UID) from user 

Get the result of the query:

4

Statistic Minimum Data
the SELECT min () from syntax is used to count a field's minimum data from a datasheet.
Grammar:

SELECT MIN (column) from Tb_name

Please refer to MAX () for specific usage.
Description
The above statistical query includes common field query can be mixed use:

SELECT MAX (UID) as Max,min (UID) as MIN,AVG (UID) as AVG from user 

The results of the query are as follows:

Max min avg
4 1 2.5000

However, you need to pay attention to the statistical query and normal field query when the results are often not expected. For example, to query the maximum user name (including UID) for the UID:

This is a wrong notation, although it is possible to execute
select MAX (UID), username
from user///This is the correct type of
select Uid,username from user order by UID DESC LIMIT 1

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.