是否有按出现频率命令mysql行的方法?
答案 0 :(得分:2)
SELECT n, count(*) as freq
FROM your_table
GROUP BY n
ORDER BY freq DESC;
更具体的例子:
SELECT n, count(*) as freq
FROM (
SELECT 1 as n
UNION ALL
SELECT 2 as n
UNION ALL
SELECT 3 as n
UNION ALL
SELECT 3 as n
UNION ALL
SELECT 3 as n
UNION ALL
SELECT 2 as n) as your_table
GROUP BY n
ORDER BY freq DESC;
将返回:
n, freq
3, 3
2, 2
1, 1