Summary two basic mysql

Aggregate function

Five common functions
COUNT: count the number of records in the table (number of rows)
the SUM: total value calculation table
AVG: the data is averaged
MAX: selecting the maximum value
MIN: for the minimum

Depending on the result of the COUNT function of different parameters, COUNT (*) will be the number of data rows contain NULL, and COUNT (<column name>) will be the number of data lines other than NULL
aggregate function will NULL excluded. But COUNT (*) exception does not exclude NULL. (Four operations if there is NULL, then the result will be NULL)

We can also use the aggregate function to delete duplicates (keyword DISTINCT)
DISTINCT written in brackets, meaning that deduplicate data before calculating the number of lines, if placed outside the parentheses, then, it does not make sense!
Example: SELECT COUNT (DISTINCT <column name>) FROM <table name>; other functions are also the same syntax!
GROUP BY clause is to group them meaning
syntax: SELECT <column name> FROM <table name> GROUP BY <column name>; multiple columns separated by commas!
GROUP BY clause is cut with a knife points table, according to the type divided into several small pieces behind your GROUP BY column name, GROUP BY writing requirements must be written later in the FROM
(if there is need to write the words of the WHERE clause in the WHERE after clause). In general writing order SELECT - FROM - WHERE --GROUP BY
order SQL clause can not be changed, can not replace each other!

In the case where the polymerization key (GROUP BY column name specified) contain NULL, NULL as he would a particular set of data! The results will be manifested in the form of lines (blank lines) is "uncertain"!

If the WHERE clause and a GROUP BY clause, then, if it is the first to use the SELECT statement WHERE clause to filter the records!
SELECT statement executes the order and with GROUP BY and WHERE! FROM - WHERE - GROUP BY - SELECT

Aggregate functions and the GROUP BY clause relating to common errors
1. When using the GROUP BY clause, SELECT clause column name can not appear outside the polymerization key
2. SELECT earlier study in which write an alias by AS, but the use of GROUP BY the time when you can not use an alias! Though not an error, but still does not support the wording!
Results 3.GROUP BY clause can not be sorted
4. aggregate functions can not be used in the WHERE clause! Only in the SELECT clause and a HAVING clause (a GROUP BY clause and) a polymerization function can be used!

HAVING clause specifies conditions for polymerization results!
Aggregate functions can be used in the SELECT clause, HAVING clause, and ORDER BY clauses.
HAVING clause to be written after the GROUP BY clause.
The WHERE clause specifies the data line condition, HAVING clause, the packet for the specified conditions.
HAVING syntax: SELECT <column name> FROM <table name> GROUP BY <column name> HAVING <grouping result corresponding to Condition> The polymerization can function

Use HAVING clause SELECT statement sequence
SELECT-FROM-WHERE-GROUP BY -HAVING (HAVING clause GROUP BY clause to write behind)

Incorrect wording SELECT product? _Type, CONUNT (* ) FROM Product GROUP BY product_type HAVING product_name = 'aaa'
If and when a column is not included in the GROUP BY clause is not allowed to write in the HAVING clause,
HAVING child with respect sentence written in the more suitable conditions of the WHERE clause, the key corresponding to the polymerization conditions, may be written in the HAVING clause and WHERE clause can be written!
= WHERE clause conditions specified row corresponding
HAVING clause = specified group corresponding to the conditions
of polymerization can be written in the key recommended write in the WHERE WHERE clause, the WHERE clause specifies the conditions on the one hand, because of the data prior to ordering been filtered, it is possible to reduce the amount of data to sort,
on the other hand can create an index to the WHERE clause specifies the column corresponding to the condition, but also can improve the processing speed! A very popular DBMS improve performance when you create an index method!

ORDER BY clause can sort query results, using the keyword ASC ascending order behind the ORDER BY clause column name, DESC descending order!
ASC (ascendent ascending) DESC (descendent decline of)
the ORDER BY clause can specify multiple sort keys, when ordering key contains NULL, will be summarized at the beginning or at the end!
It can use an alias defined in the SELECT clause column! It may also be used, or a polymerization column did not appear to function! But you can not use columns of numbers!
SELECT clause is executed after the GROUP BY clause in order, before the ORDER BY clause, therefore, in the implementation of the GROUP BY clause, the alias defined in the SELECT statement is not recognized!
For the implementation of the ORDER BY clause in it after the SELECT, you do not have this problem!
DETAILED syntax:
the SELECT <column name> FROM <table name> ORDER BY <sort reference column> (Column name + l l Descending The descending column names!) Using a plurality of columns separated by commas!
Writing order: SELECT the -from-the WHERE-the GROUP BY -HAVING-the ORDER BY
the ORDER BY usually written at the end of the SELECT statement! When using the default sort ascending sort order ORDER BY clause is not specified!

If you want to order goods for more detailed sort, just add a sort key! Separated by commas! When the rule priority from left to right, if the same value is left column, followed by a reference in the right! You can also use three or more!

Guess you like

Origin www.cnblogs.com/yeapy/p/11578088.html