2013-07-05 107 views
1

我有一個mySQL數據庫,我使用PHP/HTML頁面查詢。使用mySQL查詢頂級貢獻者

我的數據庫是如下:

+--------+-----------------+ 
| userID | changes   | 
+--------+-----------------+ 
| bharath| kernel patch | 
| vinay | server halt fix | 
| rajiv | spelling  | 
| mary | logic change | 
| bharath| new code merge | 
+--------+-----------------+ 

等等...

我有很多的開發者修改代碼。

如何查詢mySQL數據庫,使其顯示html中排名前5位的開發人員名單如下?

Developer Changes 
bharath 45 
vinay  21 
rajiv  17 
mary  16 
+0

什麼是45,21,17和16?聽起來像你需要一個簡單的組名稱與計數順序計數desc:'SELECT userID,count(1)FROM mytable ORDER BY count(1)DESC' –

+0

http://stackoverflow.com/questions/5646748/how-to -select-top-5-max-values-in-mytable – johnny

+0

這些是用戶輸入的數量 –

回答

4

要獲得前5名,你需要聚合得到計數,由計數再下訂單,並選擇前5名:

SELECT UserId, COUNT(*) AS Changes 
FROM Yourtable 
GROUP BY UserId 
ORDER BY Changes DESC 
LIMIT 5; 
+0

太棒了!這對我有用.. –