SQL - 选择最频率的项目

时间:2011-05-11 02:34:11

标签: mysql sql

假设我有如下表格:

id | name
----------
01 | Tony
02 | Peter
03 | Tony
04 | Tony
05 | John
.. | ..
99 | David

如何使用SQL语句获取字段名称的最频率(Tony)?

3 个答案:

答案 0 :(得分:5)

有些事情:

SELECT name
FROM table_name
GROUP BY name
ORDER BY COUNT(*) DESC
LIMIT 1;

答案 1 :(得分:0)

您正在寻找的是模式。本文将介绍如何获取它(查看最后一个代码示例):

http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/calculating-mean-median-and-mode-with-sq

答案 2 :(得分:0)

SELECT Name, COUNT(*)  
FROM YourNames 
GROUP BY Name 
ORDER BY COUNT(*) DESC 
LIMIT 1