计算 DISTINCT 值的出现次数 [英] Count the occurrences of DISTINCT values

查看:42
本文介绍了计算 DISTINCT 值的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找一个 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 值的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆