Mysql 如何正确使用MAX?
这是我的问题Mysql 如何正确使用MAX?,mysql,sql,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) 这
$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)
这很接近,但高于用户输入的高分应该是9,8,7,6,5
问题是按分数排序的ASC部分似乎只是随机抽取了5个具有正确分数的位置,我希望它在高分表中抽取并发位置
select top 5 from highscores where score > $score order by score desc
我想你几乎得到了你想要的。你只需要调整一下顺序。如果您先按位置值降序排序,您将得到5个最高的数字
SELECT *
FROM highscore
WHERE score > '$score'
ORDER BY position DESC, score ASC
LIMIT 5
通过输出具有最高位置编号的结果,然后使用array_reverse命令,我最终得到了想要的结果
也许你应该用语言描述你需要什么。稍微描述一下“分数”和“位置”所包含的内容也会有所帮助。什么是团体?就我现在所知,您当前的查询已经满足了您的要求。对不起,我忘了提到它的MySQLDown无法满足我的要求
$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>";
}