2011-03-02 56 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