计算 DISTINCT 值的出现次数

2021-11-20 00:00:00 sql database distinct mysql

我正在尝试查找一个 MySQL 查询,该查询将在特定字段中查找 DISTINCT 值,计算该值出现的次数,然后按计数对结果进行排序.

I am trying to find a MySQL query that will find DISTINCT values in a particular field, count the number of occurrences of that value and then order the results by the count.

示例数据库

id         name
-----      ------
1          Mark
2          Mike
3          Paul
4          Mike
5          Mike
6          John
7          Mark

预期结果

name       count
-----      -----
Mike       3
Mark       2
Paul       1
John       1

推荐答案

SELECT name,COUNT(*) as count 
FROM tablename 
GROUP BY name 
ORDER BY count DESC;

相关文章