標籤:io sp strong 資料 on cti bs ad ef
1、用的地方不一樣
where可以用在select update delete insert......into語句中
having只能用在select語句中
2、執行順序不一樣
where的搜尋條件是在執行語句進行分組之前應用
having的搜尋條件是在分組條件之後執行的
如果where和having同事出現在語句中時 也會先執行where後執行having
3.子句有區別
where子句中的條件運算式having都可以跟,而having子句中的有些運算式where不可以跟;having子句可以用集合函數(sum、count、avg、max和min),而where子句不可以。 有些地方兩者都可以用,比如
having:select studentid, avg(score) from studentScore group by studentid having left(studentid, 1)=‘0‘
where:select studentid, avg(score) from studentScore where left(studentid, 1)=‘0‘ group by studentid
這種情況下哪個會快一點 解析:
select studentid, avg(score) from studentScore group by studentid having left(studentid, 1)=‘0‘
1、分組匯總 2、過濾非法項
left(studentid, 1)=‘0‘是個效率不很高的過濾條件,如果分組會使資料量極大減少(比如每個人有幾十門課),而且要過濾掉的只是很小一部分學生,這種寫法會有比較高的效率
wk_ad_begin({pid : 21});wk_ad_after(21, function(){$(‘.ad-hidden‘).hide();}, function(){$(‘.ad-hidden‘).show();}); if( navigator.userAgent.indexOf("MSIE 6.0") < 0 ){BAIDU_CLB_fillSlot( ‘920314‘ );}
select studentid, avg(score) from studentScore where left(studentid, 1)=‘0‘ group by studentid
1、過濾非法項 2、分組匯總
雖然left(studentid, 1)=‘0‘是個效率不很高的過濾條件,但是如果你要從幾百萬學生中找到幾十個學生3-5門功課的平均分,還是應該很明智的選擇它
where和having的區別