The scene where you can use and having

Scene 1. where having and can be used by

Share on mysql difference in where and having clauses of this paper is to share the couple are new to sql, the following will be the actual case studies:

Below a specific example to explain:

Scene 1. where having and can be used by

1)select addtime,name from dw_users where addtime> 1500000000

2)select addtime,name from dw_users having addtime> 1500000000

Explanation: The above may be having with the premise that I've screened out addtime field, in this case, and where the effects are equivalent, but if I do not select addtime will be given! ! Because the filter, then the filter having the foregoing fields, and where the screening is carried out directly from the field data table.

 

2. The situation can only be used where, not by having the

1) select addtime,name from dw_users where addtime> 1500000000

2) select phone, name from dw_users having addtime> 1500000000 // error! ! ! Because the front of the field and did not filter out addtime

3. You may only use having, not with the situation where

The average price for each query category_id goods, obtain product information is greater than the average price of $ 100

1)select category_id , avg(price) as ag from dw_goods group by goods_category having ag > 100

2) select category_id, avg (price) as ag from dw_goods where ag> 100 group by goods_category // given! ! Because this data table from dw_goods this field there is no ag

Note: keep the back of the field where the data table, if I put ag replaced avg (price) is wrong! Because the table is not the field. And having just come out of the preceding query is what you can take anything back.

Useful to feel little concern expressed thanks to the author's support.

Guess you like

Origin www.cnblogs.com/tttttwwwwww/p/11315811.html