2011-10-04 149 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