【问题标题】:How to count the distinct column in mysql?如何计算mysql中的不同列?
【发布时间】:2011-07-07 09:49:18
【问题描述】:

例如:

使用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

非常感谢!!

【问题讨论】:

    标签: mysql count distinct


    【解决方案1】:

    您可以使用GROUP BY按类型获取结果:

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

    这应该会为您提供您正在寻找的结果。

    MySQL 文档: 11.16.1 GROUP BY (Aggregate) Functions

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      相关资源
      最近更新 更多