where, having, 用法區別:
- having只能用在group by之後,對分組後的結果進行篩選(即使用having的前提條件是分組)。
- where肯定在group by 之前,即也在having之前。
- where後的條件表達式裏不允許使用聚合函數(count(),sum(),avg(),max(),min()),而having可以。
where , group by, having, order by 執行順序:
- 執行where xx對全表數據做篩選,返回第1個結果集。
- 針對第1個結果集使用group by分組,返回第2個結果集
- 針對第2個結果集中的每1組數據執行select xx,有幾組就執行幾次,返回第3個結果集。
- 針對第3個結集執行having xx進行篩選,返回第4個結果集。
- 針對第4個結果集排序。
本文來自 superhosts 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/superhosts/article/details/39298529?utm_source=copy