你如何正确使用MAX?

时间:2011-10-04 19:38:11

标签: mysql sql

这是我的查询

$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,我希望它在高分表中拉出并发位置

3 个答案:

答案 0 :(得分:1)

我认为你几乎得到了你想要的东西。你只需要调整订单。如果您首先按降序position值排序,则会获得5个最高数字。

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

答案 1 :(得分:0)

尝试

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

答案 2 :(得分: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>";
}