where和having的區別

來源:互聯網
上載者:User

標籤: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的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.