A quick review of MySQL: Summary and grouping

To the premise above: reference books "MySQL must know will be"

10.3 Summary Data

We often need to aggregate data without actually retrieved them out of office, aims to provide a special function MySQL. Using these functions, MySQL query can be used to retrieve data for analysis and report generation. Examples of this type of retrieval are the following:

  • Determining the number of rows in the table (or satisfy some conditions or the number of rows comprising a certain value).
  • Obtain rows in the table and set.
  • Find the maximum value of the table columns (or rows or certain of all rows), minimum and average.

Examples of the above-described needs in the table of data (rather than the actual data itself) are summarized. Therefore, the actual return data table is a waste of time and processing resources (not to mention the bandwidth of a).

MySQL offers five aggregate functions.

Aggregate functions (aggregate function) running in the row group, and the calculation function returns a single value.

function Explanation
AVG() Returns the average of a column
COUNT() Returns the number of rows of a column
MAX() Returns the maximum value of a column
MIN () Returns the minimum value of a column
SUM() Returns the value of a column and

Following is a brief description of these functions

Create the following table:

CREATE TABLE student(
    sid INT PRIMARY KEY,
    sname VARCHAR(20),
    ssex  CAHR(1)
);
CREATE TABLE score(
    sid INT PRIMARY KEY,
    sname VARCHAR(20),
    smath INT,
    senglish INT
);

10.3.1 AVG () function

The AVG () by counting the number of rows of the table and calculates the sum of a particular column, the column average value was obtained. The AVG () is used to return the average of all the columns, the column can be used to return a specific average.

The AVG () can be used to determine the average value of a particular column, and the column name must be given as a function parameter. In order to obtain an average value of a plurality of columns, you must use a plurality of the AVG () function.
The AVG () functions ignore row column value is NULL.

To use AVG () function to query the average score in math achievement table of (their own plus a few random number):

SELECT AVG(smath) AS avg_smath FROM score;

The AVG () can also be used to determine the average value of a particular column, with the WHERE clause.

10.3.2 COUNT () function

COUNT () function determines the number of rows that meet specific criteria, or rows of a table.

COUNT () function There are two ways to use:

  • Use COUNT (*) for counting the number of rows in a table, regardless of the table column contains a null value (NULL) or non-null value.
  • Use COUNT (column) having a row of a particular column value counts NULL value is ignored.

For example: Returns the total number of students:

SELECT COUNT(*) FROM student;

Returns the total number (specific columns) girls:

SELECT COUNT(ssex) FROM student WHERE ssex='女';

If you specify a column name specified column is empty row is COUNT () function is ignored, but if COUNT () is a function with an asterisk (*), is not negligible.

10.3.3 MAX () function

MAX () returns the largest value in the specified column. MAX () requires you to specify the column name.

For example: Back to the results table the highest score in math achievement:

SELECT MAX(smath) FROM score;

Although MAX () is generally used to find the maximum value or date value, but allows it to MySQL returns the maximum value in any column, including the text column returns the maximum value. When text data, if the data is sorted according to the corresponding column, the MAX () Returns the last row. MIN () function is, but on the contrary, the return of the front row.

MAX (), MIN () are ignored row column value is NULL.

10.3.4 MIN () function

MIN () returns the minimum value in the specified column. MIN () requires you to specify the column name.

For example: Return transcript lowest score in math achievement:

SELECT MIN(smath) FROM score;

10.3.5 SUM () function

The SUM () is used to return the value of the specified column and the (total).

Such as the return of a total score in math classes in:

SELECT SUM(smath) FROM score;

Using standard arithmetic operators, aggregate functions can all be used to perform calculations on a plurality of columns.

The SUM () function ignores row column value is NULL.

10.3.6 gather different values

5 or more aggregate functions can be used as follows:

  • Performing calculation for all rows, or the designated parameter to the parameter (MAX and MIN);
  • Contains only different values, specify DISTINCT parameters. (De-emphasis)

For example DISTINCT parameter specifies the usage of the following: For example, the results in the table of average math scores, the average score of only considering the different points:

SELECT AVG(DISTINCT smath) AS avg_smath FROM score;

If you specify a column name can only be used DISTINCT COUNT (), DISTINCT can not be used COUNT (*), and therefore does not allow COUNT (DISTINCT), or an error. Similarly, DISTINCT column name must be used, or not used to calculate expressions.

DISTINCT can be used MAX () and MIN (), but does not make sense.

10.3.7 combined aggregate functions

In fact SELECT statement can contain multiple aggregate functions as needed .

Use aggregate functions, it is best to use an alias.


Summary: aggregate functions to aggregate data, these functions are efficiently designed, they return the results in general than your own client computing applications much faster.

10.4 Data Packet

As already we learned SQL aggregate functions to summarize data. However, such as: Requires a student table, how many people have boys and girls. Available use two statements were printed. Here's another way: the packet.

Packet: the data into a plurality of logical groups, so as to enable aggregation was calculated for each group.

10.4.1 Creating groups

When grouping established in the GROUP BY clause of the SELECT statement. According to the above example, for boys and girls students watch grouping:

SELECT ssex, COUNT(ssex) AS total FROM student GROUP BY ssex;

Output:

+---------+----------------+
| stu_sex |      total     |
+---------+----------------+
| 女      |              6 |
| 男      |             11 |
+---------+----------------+
3 rows in set (0.05 sec)

But it can also be used like this:

SELECT ssex, COUNT(*) AS total FROM student GROUP BY ssex;

The output above.

Why is this so? ? Because the use of GROUP BY, you do not have to specify the calculation and valuation of each group was. The system automatically. GROUP BY clause indicating MySQL data packet, and then aggregated for each group, rather than the entire result set.

Before the specific use of the GROUP BY clause, need to know some important provisions:

  • GROUP BY clause may include any number of columns. This enables nesting of the packet, to provide more detailed control data packets.
  • If the packet is nested in the GROUP BY clause, data packets will be aggregated on the last predetermined. In other words, when the establishment of a packet, all columns are designated with the calculation (you can not retrieve data from the individual columns).
  • Each column GROUP BY clause must be listed in the column or retrieve valid expression (but are not aggregate functions). If you use an expression in a SELECT, you must specify the same expression in the GROUP BY clause. You can not use an alias.
  • In addition to aggregate calculation statement, the SELECT statement in each column must be given in the GROUP BY clause.
  • If the packet is a NULL value in the column, as a NULL packet is returned. If a NULL value in the column a plurality of rows, they will be divided into one group.
  • After the GROUP BY clause must appear in the WHERE clause, ORDER BY clause before.

Using WITH ROLLUP keyword, you can get a summary of each packet and each packet level value (for each group) are
for example:

SELECT ssex, COUNT(*) AS total 
FROM student 
GROUP BY ssex WITH ROLLUP;

Output:

+---------+-------+
| stu_sex | total |
+---------+-------+
| 女      |    10 |
| 男      |    16 |
| NULL    |    26 |
+---------+-------+
3 rows in set (0.05 sec)

Introducing a function: IFNULL (expression, alt_value): IFNULL () function for determining whether the first expression is NULL, if the second parameter is NULL value is returned if it is not NULL is returned by the first parameter value.

SELECT IFNULL(ssex,'总计')  AS ssex, COUNT(*) AS total 
FROM student 
GROUP BY ssex WITH ROLLUP;

Output:

+---------+-------+
| stu_sex | total |
+---------+-------+
| 女      |    10 |
| 男      |    16 |
| 总计    |    26 |
+---------+-------+
3 rows in set (0.05 sec)

10.4.2 Packet Filter

MySQL allows filtering packets, which packets include a predetermined, which exclude packets. But the WHERE clause is specified filter line rather than grouping. So MySQL provides the HAVING clause, HAVING clause similar to WHERE, so far all types of WHERE clause HAVING clause can be used instead. The only difference is the WHERE clause line filter, and the filter packet HAVING clause (i.e., the first packet is then filtered) .

For example: the number of girls or boys show students the table is greater than 15.

SELECT ssex, COUNT(*) AS total 
FROM student 
GROUP BY ssex HAVING COUNT(*)>=15;

Note: HAVING clause after the GROUP BY, because it is the result of a packet filter. The WHERE clause is a rows filtered, it must be before the GROUP BY. So WHERE clause filters rows may affect the results of a GROUP BY grouping .

You can use both WHERE and HAVING clauses do? ? The answer is yes.

10.4.3 Grouping and Sorting

Talk about the sort (ORDER BY) and grouping (GROUP BY) difference:

Sequence Packet
Sort the output produced Output packet order may not be
Any column can be used (even non-selected columns can also be used) Use only select the column or expression column, and must use each of the selected column expression
Not necessarily need If you use the column together with the aggregate functions (or expression), you must use

The first row of the above table, the data can often find is indeed GROUP BY packet output packet order, but this is not always the case, it is not required SQL specification. Further, the user may require different from the sequential ordering of packets. Just because you packet data in a certain way, it does not mean you need to be the same sort of way output. It should provide a clear BRDER BY clause, even if its effect is equivalent to the output of the GROUP BY clause.

It is generally best to keep up with the GROUP BY clause ORDER BY clause.

So how in the position to place: ORDER BY clause must be the last statement on sQL.

So modify the above statement: the example cited was not very good, take a look at how to write.

SELECT ssex, COUNT(*) AS total 
FROM student 
GROUP BY ssex 
HAVING COUNT(*)>=15
ORDER BY ssex;

Summary: Learn how to combine data calculated using the SQL aggregate functions. GROUP UP also describes how to use these aggregated gold clause acid on the data set, return the results for each group. To say nothing of filtering HAVING clause specific group, and the difference between WHERE and ORDER BY HAVING GROUP BY and

Published 57 original articles · won praise 18 · views 8741

Guess you like

Origin blog.csdn.net/weixin_41800884/article/details/104025145