2010-09-24 213 views
4
2.8.7 
2.8.3 
2.8.2 
2.8.7 
2.8.5 
2.8.7 
2.8.7 
2.8.5 
2.6.0 
2.8.3 
2.6.4 
2.6.3 
2.8.4 
2.8.0 
2.6.3 
2.8.5 
2.8.5 
2.8.5 
2.6.0 
2.8.2 

如何爲這些版本的數量排序一個獨特的價值版本?MySQL order by COUNT DISTINCT

在出口我希望得到以下幾點:

2.8.5 5

2.8.7 4

2.6.0 2

2.6.3 2

2.8.2 2

2.8.3 2

2.8.4 2

2.6.4 1

2.8.0 1

ORDER BY計數獨特的版本))

對不起我的英文不好

回答

12
SELECT 
    version, 
    COUNT(*) AS num 
FROM 
    my_table 
GROUP BY 
    version 
ORDER BY 
    COUNT(*) DESC 
+0

適合我的要求。 – 2014-02-13 12:22:09

3
SELECT version, COUNT(*) FROM tablename 
GROUP BY version 
ORDER BY COUNT(*) DESC; 

或備選語法

SELECT version, COUNT(*) FROM tablename 
GROUP BY 1 
ORDER BY 2 DESC; 
1
select version, count(*) from sometable group by version order by count(*) desc; 
-1

不用擔心,你的英語是可以理解的。請在下面找到答案:

> SELECT version, COUNT(version) AS quantity 
FROM tablename 
GROUP BY version 

欲瞭解更多詳情,請參閱答案在http://www.kelvinese.com