我正在尝试查找一个 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;
这篇关于计算 DISTINCT 值的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!