MySQL COUNT 與 GROUP BY,mysqlcount

來源:互聯網
上載者:User

MySQL COUNT 與 GROUP BY,mysqlcount

group by 用來對結果進行分組,分組之後便於統計,所以總會有count跟著出現

例如下表:


按照ID分組,統計每個ID重複了幾次

SELECT yww_test.id,COUNT(id) as nFROM yww_testGROUP BY yww_test.id


把重複的記錄找出來可以像下面這樣

SELECT id,COUNT(*) AS nFROM yww_testGROUP BY yww_test.idHAVING COUNT(*)>1;

其中count(*)是統計每一組有多少行

注意:

count(*)將返回表格中所有存在的行的總數包括值為null的行,然而count(列名)將返回表格中除去null以外的所有行的總數(有預設值的列也會被計入).
distinct 列名,得到的結果將是除去值為null和重複資料後的結果

參考:http://www.111cn.net/database/mysql/53202.htm

更多group by請參考:http://blog.csdn.net/xxpyeippx/article/details/8059910



相關文章

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.