我目前正在开发一个PHP应用程序,它对MySQL的一组结果进行排名,然后在网页上显示它们。我制作了三个MySQLi查询,然后以排名显示结果。一切似乎都很好但在某些时候(没有常规模式),显示的结果有时会显示0作为第一个等级而不是1,有些则显示为1。
我已经尝试调试过去一个月的代码,但没有进展,我不得不重写在不同场合完全执行查询的代码,但零0问题仍然存在。
以下是我的代码:
这是第一部分:
$sql0 = "SET @rank = 0, @prev_val3 = NULL;";
$pull_report_details5 = "SELECT rank, score, student_number, subject_name, gender FROM (
SELECT @rank := IF(@prev_val3=score,@rank,@rank+1) AS rank, @prev_val3 := score AS score, student_number, subject_name, gender
FROM uploaded_results WHERE `subject_name` = '$subject_name' ORDER BY score DESC
) as result";
mysqli_query($dbc, $sql0)or mysql_error();
$push_pull_report_details5 = mysqli_query($dbc, $pull_report_details5)or die("Error pulling report details");
第二部分:
$sql1 = "SET @rank2 = 0, @prev_val2 = NULL;";
$pull_report_details3 = "SELECT rank2, score, student_number, subject_name, gender FROM (
SELECT @rank2 := IF(@prev_val2=score,@rank2,@rank2+1) AS rank2, @prev_val2 := score AS score, student_number, subject_name, gender
FROM uploaded_results WHERE `subject_name` = '$subject_name' AND `gender` = 'Male' ORDER BY score DESC
) as result";
mysqli_query($dbc, $sql1)or mysql_error();
$push_pull_report_details3 = mysqli_query($dbc, $pull_report_details3)or die(mysqli_error($dbc)."failure");
第三部分:
$sql2 = "SET @rank1 = 0, @prev_val = NULL;";
$pull_report_details4 = "SELECT rank1, score, student_number, subject_name, gender FROM (
SELECT @rank1 := IF(@prev_val=score,@rank1,@rank1+1) AS rank1, @prev_val := score AS score, student_number, subject_name, gender
FROM uploaded_results WHERE `subject_name` = '$subject_name' AND `gender` = 'Female' ORDER BY score DESC
) as result2";
mysqli_query($dbc, $sql2)or mysql_error();
$push_pull_report_details4 = mysqli_query($dbc, $pull_report_details4)or die("Error pulling report details");
最后,我在三个不同的表上显示三个查询。