mysql - 如何统计mysql中的非重复列?

标签 mysql count distinct

例如:

使用 select query 我得到以下结果:

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

使用DISTINCT我只得到了distinct result,但我还想得到每个distinct的总数

现在我想获得总数

typea,typeb,typec and typed.

就像:

columnB total
typea   2
typeb   1
typec   1
typed   3

非常感谢!!

最佳答案

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

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

这应该会给您准确的结果。

MySQL Documentation: 11.16.1 GROUP BY (Aggregate) Functions

关于mysql - 如何统计mysql中的非重复列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5170147/

相关文章:

MySQL - 获取缺失的 2 列 ID

mysql - 错误 1215 : Cannot add foreign key constraint - MySQL (Simple Tables)

sql - postgres 以类似条件计数

php - Codeigniter:两表查询

php - 使用 SELECT NOT IN 查找不存在的 ID?

mysql - 如何包含 MySQL COUNT 为 0 的行?

SQL 记录出现次数

python - Pandas 'count(distinct)' 等值

php - 使用 DISTINCT 和 UNION 在 SQL 查询中获取字段名称

mysql - #1241 - 操作数应包含 1 列 IN