2014-02-26 16 views
0

我有一個遊戲,我將每個點保存到每個玩家。 現在我想建立一個高分榜,它列出了最好成績的球員。但我只希望每名球員的最高分數可見。PHP SQL用戶組

例:

David 38 points 
Elin 25 points 
Kelly 3 points 

而不是:

David 38 points 
David 35 points 
Elin 25 points 
Elin 23 points 
Elin 20 points 
etc etc 

我今天代碼:

$sql="select userID, poang from floppy ORDER BY poang DESC LIMIT 10"; 
    $result=mysql_query($sql) or die(mysql_error()."<br />".$sql); 
    while($row = mysql_fetch_array($result)){ 
echo $row[userID]." ".$row['poang']." points<br />"; 
} 

任何人都知道該怎麼辦?

+1

什麼是列名於「大衛,艾琳」? – Dev

+1

'SUM'或'MAX'(取決於你想要的)+'GROUP BY' – Brice

+0

你在你的問題中得到了'group by',但是在你的查詢中沒有,你是否嘗試過'select userID,poang FROM floppy ORDER BY poang DESC GROUP BY userID LIMIT 10'? – Jurik

回答

3
SELECT TOP 10 userID, MAX(poang) from floppy GROUP BY userID ORDER BY MAX(poang) DESC 
+0

嗨, 使用此代碼: $ sql =「從軟盤GROUP BY用戶ID選擇TOP 10 userID,MAX(poang)ORDER BY MAX(poang)DESC」; 我得到了此錯誤: 您的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行使用來自軟盤GROUP BY用戶ID ORDER BY MAX(poang)DESC''10 userID,MAX(poang)附近的正確語法 – Andreas

+0

您沒有指定它是mysql ,TOP是TRANSACT SQL,因爲我認爲它是SQL Server。取消TOP 10並添加LIMIT 10。 – Oscar

1

使用組由只取一個& MAX()爲最大值

+0

嗨,我得到這個: 你的SQL語法有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在第1行的'Group By userID'附近使用正確的語法。 從軟盤中選擇userID,poang ORDER BY MAX(poang)DESC LIMIT 10 Group By userID – Andreas