2011-10-04 148 views
-2

這裏是我的查詢如何正確使用MAX?

$gethigherrows = "(SELECT * 
    FROM highscore 
    WHERE score >= '$score' 
ORDER BY score ASC, 
position ASC 
    LIMIT 5)"; 

這是我想要的東西,包括:

SELECT * FROM highscore WHERE score > '$score' AND position (is the highest 5 numbers of that group) 

enter image description here

已經很接近了,但高分用戶條目上方應該是9,8, 7,6,5

問題是ORDER BY score ASC部分好像只是隨機抽出5個有正確分數的我想要它拉兼任職務的高評分表

+2

也許你應該用文字描述你需要什麼。對「分數」和「位置」含義的一點描述也會有所幫助。什麼是團隊?據我現在可以看到,您目前的查詢已經完成了你所要求的。 – GolezTrol

回答

0

我終於得到了我想要的結果,輸出結果的位置號碼最高,然後使用array_reverse()命令。

 $gethigherrows = "(SELECT * 
FROM highscore 
    WHERE score >= '$score' 
    ORDER BY position DESC 
LIMIT 5)"; 

$getlowerrows = "(SELECT * 
    FROM highscore 
    WHERE score < '$score' 
ORDER BY score DESC, 
position ASC 
    LIMIT 5)"; 

$higherrows= mysql_query($gethigherrows); 
$lowerrows= mysql_query($getlowerrows); 

if(mysql_error())echo mysql_error(); 

$rows = array(); 
while($row = mysql_fetch_array($higherrows)){ 
    $rows[] = $row; 
} 

$revrows = array_reverse($rows); 

foreach($revrows as $row) 
{ 
    $uppertable .= "<tr><td>$row[position]</td><td>$row[name]</td><td>$row[score]</td></tr>"; 
} 
0

嘗試

select top 5 from highscores where score > $score order by score desc 
+0

對不起,我忘了提及它的MySQL – Tules

1

我想你已經幾乎得到了你想要的東西。你只需要調整順序。如果您首先按降序position排序,則會得到5個最高的數字。

SELECT * 
    FROM highscore 
    WHERE score > '$score' 
    ORDER BY position DESC, score ASC 
    LIMIT 5 
+0

不給我我不想要的東西 – Tules