如何计算mysql中的distinct列?

qin*_*ang 4 mysql count distinct

例如:

随着select query我得到以下结果:

columnA columnB
type1   typea
type2   typea
type3   typeb
type4   typec
type5   typed
type6   typed
type7   typed
Run Code Online (Sandbox Code Playgroud)

与DISTINCT我只拿到了distinct result,但我也希望得到total number各的不同.

现在我想要得到的总数的

typea,typeb,typec and typed.
Run Code Online (Sandbox Code Playgroud)

就像:

columnB total
typea   2
typeb   1
typec   1
typed   3
Run Code Online (Sandbox Code Playgroud)

非常感谢你!!

And*_*ore 9

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

SELECT columnB, COUNT(*) AS 'total'
  FROM myTable
  GROUP BY columnB;
Run Code Online (Sandbox Code Playgroud)

这应该可以为您提供您想要的结果.

MySQL文档: 11.16.1 GROUP BY(聚合)函数