[Mysql] Mysql data packets and GROUP BY HAVING, and use of combination WHERE

Appreciated that packets can be: the GROUP BY clause of a following column names are grouped , and each packet rather than the entire operating table.

Example: In the product table, the number of searches each vendor's goods.

mysql> SELECT vend_id,COUNT(*) AS num_prods FROM products GROUP BY vend_id;

The results:
+ --------- + ----------- +
| vend_id | num_prods |
+ --------- + --------- - +
| 1001 | 3 |
| 1002 | 2 |
| 1003 | 7 |
| 1005 | 2 |
+ ----------- + --------- +
4 rows in the SET (0.01 sec)

analysis:

The first group vend_id, then COUNT is performed for each aggregate packet. When the purpose is to search for every time a record is retrieved, think with GROUP BY, for example here is for each vendor.

GROUP BY provides:

1, GROUP BY later may comprise a plurality of columns, which is nested.

2, if the GROUP BY been nested, the data will be summarized in the last packet.

3, each of the columns in the GROUP BY clause listed must be valid or column retrieval expression (but not aggregate function), if used in the SELECT expression, the same must be specified in the GROUP BY clause expression. You can not use an alias.

4, except that the outer gathering statement, the SELECT statement in each column must be given in the GROUP BY clause.

5, if a packet with a NULL value in the column, as a NULL packet is returned. NULL if there are a plurality of columns, which is returned as a packet.

6, the GROUP BY clause must come after the WHERE clause, ORDER BY clause before.

Packet filter Results

We know WHERE clause is used to filter the results, but for packet filtering WHERE clause is not.

Because the WHERE clause is a filter for the line. To filter group results, you must use the HAVING clause, HAVING clause can be filtered for the result of the grouping.

For example:

In the order, the id and retrieve the orders of customers with more than two orders.

analysis:

In this retrieval needs, the need to be grouped according to the customer id, then filtered packets is greater than 2 orders.

mysql> SELECT cust_id,COUNT(*) AS orders FROM orders GROUP BY cust_id HAVING orders>=2;

The results:
+ --------- + -------- +
| cust_id | the Orders |
+ --------- + -------- +
| 10001 | 2 |
+ --------- + -------- +
1 Row in the SET (0.00 sec)

WHERE combination with (before filtered WHERE)

Sometimes, the GROUP BY and WHERE clauses should be used in combination . For example: in the product table is retrieved to provide two or more commodity, and the price is higher than 10 suppliers.

analysis:

First, search suppliers, and therefore it should be the SELECT clause SELECT vend_id

Secondly, the product table, this column has a price, so to filter price is higher than 10 conditions to be used in the WHERE clause . SELECT vend_id FROM prodcuts WHERE prod_price> = 10.

Next, vend_id group, can be obtained so that the price of each commodity number vend_id higher than 10, the WHERE clause GROUP BY placed. SELECT vend_id FROM prodcuts WHERE prod_price> = 10 GROUP BY vend_id.

Finally, the result of packet filtering, filter out two or more packets of goods

mysql> SELECT vend_id,COUNT(*) AS num_prods FROM products WHERE prod_price>=10 GROUP BY vend_id HAVING COUNT(*)>=2;

The results:
+ --------- + ----------- +
| vend_id | num_prods |
+ --------- + --------- - +
| 1003 | 4 |
| 1005 | 2 |
+ ----------- + --------- +
2 rows in the SET (0.00 sec)

Sort of grouping results

In order schedule, retrieved order number, and the total price of the line 50 is equal to the total price of the order

mysql> SELECT order_num,SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price)>=50 ORDER BY ordertotal;

The result is:
+ ----------- + ------------ +
| order_num | OrderTotal |
+ ----------- + --- + ---------
| 20006 | 55.00 |
| 20008 | 125.00 |
| 20005 | 149.87 |
| 20007 | 1000.00 |
+ ----------- + ------- + -----
4 rows in the SET (0.08 sec)

The order of the SELECT clause

SELECT

FROM

WHERE

GROUP BY

HAVING

ORDER BY

LIMIT
---------------------
Transfer: https: //blog.csdn.net/liuchunming033/article/details/47279003 for personal learning, there are changes

Guess you like

Origin www.cnblogs.com/xuelisheng/p/11363799.html