MYSQL SELECT语句中最常见的数字

时间:2012-05-25 20:14:19

标签: mysql select mode

我正在尝试使用MYSql语句来吐出字段中最常见的数字。我相信我应该使用COUNT(QUANTITY),但我对GROUP BYORDER BY感到困惑,我似乎无法获得正确的模式(最常见的数字)。

*的 修改 *

以下是一个示例表:

QUANTITY | ORDER_NUMBER
   1         51541
   4         12351
   5         11361
   5         12356
   6         12565
   8         51424
   10        51445
   25        51485

MYSql语句应该吐出数字 5 ,因为它最常出现

4 个答案:

答案 0 :(得分:4)

SELECT QUANTITY,COUNT(*)
FROM ...
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;

答案 1 :(得分:2)

SELECT ORDER_NUMBER AS ORDER, COUNT(QUANTITY) as numorders
FROM table 
GROUP BY ORDER_NUMBER
ORDER BY numorders

答案 2 :(得分:1)

获得前10名order_number

select order_number, count(order_number) as quantity
from your_table
group by order_number
order by quantity desc
limit 10

答案 3 :(得分:0)

SELECT QUANTITY, COUNT(QUANTITY) AS TOTAL_Q
 FROM MYTABLE
 GROUP BY QUANTITY
 ORDER BY TOTAL_Q DESC

这将为您提供从最多到最少数量的数量......