5

我试图让一个 MYSql 语句吐出一个字段中最常见的数字。我相信我应该使用COUNT(QUANTITY),但我很困惑 toGROUP BYORDER BY,我似乎无法获得正确的 MODE (最常见的数字)。

*编辑*

这是一个示例表:

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

MYSql 语句应该吐出数字5,因为它出现的频率最高

4

4 回答 4

4
SELECT QUANTITY,COUNT(*)
FROM ...
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
于 2012-05-25T20:16:25.240 回答
2
SELECT ORDER_NUMBER AS ORDER, COUNT(QUANTITY) as numorders
FROM table 
GROUP BY ORDER_NUMBER
ORDER BY numorders
于 2012-05-25T20:16:00.150 回答
1

获得前 10order_number

select order_number, count(order_number) as quantity
from your_table
group by order_number
order by quantity desc
limit 10
于 2012-05-25T20:16:55.160 回答
0
SELECT QUANTITY, COUNT(QUANTITY) AS TOTAL_Q
 FROM MYTABLE
 GROUP BY QUANTITY
 ORDER BY TOTAL_Q DESC

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

于 2012-05-25T20:25:44.313 回答