SQL之WHERE,GROUP,HAVING,ORDER BY

①WHERE is set to filter data

②GROUP BY packet is a query, the result set is grouped, and the polymerization is generally GROUP BY function in conjunction with:

GROUP BY a principle that all the columns behind the SELECT, the function does not use a polymerization column, later to be in the group by (important), as follows:

SELECT column_A,column_B,SUM(column_C) FROM Orders
GROUP BY column_A,column_B; 

③HAVING result for the packet filter. (I.e., on the premise that a packet HAVING)

④ORDER BY result set is to sort

Detailed:

1.WHERE must before GROUP BY

2.GROUP BY must precede HAVING.

3.HAVING can only be used after GROUP BY, the results grouped screening. (Provided that the use of HAVING a packet)

 

For example:

When a query also appeared WHERE, GROUP BY, HAVING, ORDER BY, the order of execution of the order is written and select - from - where - group - having - select - order by:

1. Perform screening WHERE clause to do full table of data, returns a result set

2. GROUP BY result set for the first packet, a second result set returned

3. screened for the second result set to use HAVING, returns the third result set

4. performed for the third set of results for each set of data SELECT XX, several groups have performed several times to return to the fourth set of results

The fourth result set sorting using OEDER BY

 

WHERE and HAVING difference:

WHERE clause can not use the packet function (!!!)

Grouping function can be used in the HAVING clause

WHERE execution order before HAVING, since WHERE clause before the GROUP BY clause, HAVING clause after the GROUP BY

HAVING clause is the result of the packet filter, so use HAVING premise is the use of GROUP BY

Guess you like

Origin www.cnblogs.com/yaoze2018/p/11266919.html