这个PHP / Mysql代码我哪里出错了?

时间:2012-09-24 20:14:33

标签: php mysql

以下代码仅适用于第一个值(puzzle_level == 4),它应该做的是打印出puzzle_level == 4,5,6& 7 :(

    $result = mysql_query("
    SELECT puzzle_level, max(final_score) as highest_final_score
        FROM sleuth_game
        WHERE playerID='$_SESSION[customerID]' and final_score > 1
        GROUP BY puzzle_level
        ORDER BY puzzle_level");  //Load in a the highest scores
    while($row = mysql_fetch_array($result)) {
        echo "<p>".$row['highest_final_score']."</p>";
    }

编辑: 在数据库中,所有游戏都是puzzle_level 4,5,6或7.我希望的输出是玩家获得的每个puzzle_level的最高分,但我写的代码只显示puzzle_level == 4的最高分数

1 个答案:

答案 0 :(得分:1)

您希望在SQL查询的WHERE语句中使用'final_score&gt; 1'实现什么目标?

SELECT puzzle_level, max(final_score) as highest_final_score
FROM sleuth_game
WHERE playerID='$_SESSION[customerID]' and final_score > 1
GROUP BY puzzle_level
ORDER BY puzzle_level

也许HAVING子句是你想要实现的目标的解决方案:

SELECT puzzle_level,MAX(final_score) AS highest_final_score
FROM sleuth_game
WHERE playerID='$_SESSION[customerID]'
GROUP BY puzzle_level
HAVING MAX(final_score)>1
ORDER BY puzzle_level