mysql 自我總結

來源:互聯網
上載者:User

group by  和  DISTINCT

          group by按組劃分,每組只顯示1條記錄

          distinct後接多個欄位,保證每個欄位組合的記錄唯一(distinct id,name from )

groupby having 和 where 

         SELECT *,(SUM(win)/COUNT(*)) as present  FROM `xxtable`  GROUP BY hero HAVING present>0.1

SELECT *,(SUM(win)/COUNT(*)) as present  FROM `xxtable` where present>0.1 GROUP BY hero (語句錯誤,找不到present)

where 子句的作用是在對查詢結果進行分組前,將不符合where條件的行去掉,即在分組之前過濾資料,條件中不能包含聚組函數,使用where條件顯示特定的行

having用在彙總函式中,判斷彙總函式(sum,count,avg,)的值是否大於某個值




執行個體特訓:

1.英雄聯盟多玩魔盒

A:擷取勝率排名前2的英雄和對局

SELECT *,(SUM(win)/COUNT(*)) as present  FROM `aa` WHERE userid=1 GROUP BY hero ORDER BY present desc limit 2

B:擷取勝率大於0.5的英雄和對局

SELECT *,(SUM(win)/COUNT(*)) as present  FROM `aa` WHERE userid=1 GROUP BY hero HAVING present>0.5 ORDER BY present desc

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.