2011-03-02 57 views
4

E.g:如何計算mysql中的不同列?

select query我得到以下結果:

columnA columnB 
type1 typea 
type2 typea 
type3 typeb 
type4 typec 
type5 typed 
type6 typed 
type7 typed 

DISTINCT我只得到了distinct result,但我也希望得到每個不同的total number

,現在我想要得到的總數

typea,typeb,typec and typed. 

就像:

columnB total 
typea 2 
typeb 1 
typec 1 
typed 3 

非常感謝您!

回答

9

可以使用GROUP BY按類型,得到的結果:

SELECT columnB, COUNT(*) AS 'total' 
    FROM myTable 
    GROUP BY columnB; 

這應該給你正是你正在尋找的結果。

MySQL文檔:11.16.1 GROUP BY (Aggregate) Functions

+0

馬麗娟!我得到了它的工作。非常感謝你! – qinHaiXiang 2011-03-03 00:32:24