gpt4 book ai didi

php - 数据未正确来自数据库

转载 作者:行者123 更新时间:2023-11-29 09:38:25 26 4
gpt4 key购买 nike

我尝试制作学生成绩管理系统。当我试图根据每个学生在考试中获得的总分来给他们排名时,我遇到了问题。在我的输出中,我得到第一(获得分数 450)、第二(获得分数 449)、第四(获得分数 448)。第三个位置缺失。 5号、10号以及其他一些位置缺失。但我在我的代码中没有发现任何问题。

我的数据库

enter image description here

if (!function_exists('get_position_in_exam')) {

function get_position_in_exam($school_id, $exam_id, $class_id, $section_id, $mark) {


$ci = & get_instance();
$sql = "SELECT id, total_obtain_mark, FIND_IN_SET( total_obtain_mark,(
SELECT GROUP_CONCAT( total_obtain_mark ORDER BY total_obtain_mark DESC )
FROM exam_results WHERE school_id = $school_id AND exam_id = $exam_id AND class_id = $class_id AND section_id = $section_id ))
AS rank
FROM exam_results
WHERE school_id = $school_id AND exam_id = $exam_id AND class_id = $class_id AND section_id = $section_id AND total_obtain_mark = $mark";

$rank = @$ci->db->query($sql)->row()->rank;

if($mark == 0){
return '--';
}

if($rank == 1){
return $rank.'st';
}elseif($rank == 2){
return $rank.'nd';
}elseif($rank == 3){
return $rank.'rd';
}elseif($rank > 3 ){
return $rank.'th';
}else{
return '--';
}
}

}

最佳答案

您的子查询中需要不同的total_obtain_mark

GROUP_CONCAT(不同的total_obtain_mark ORDER BYtotal_obtain_mark DESC)

"SELECT id, total_obtain_mark, FIND_IN_SET( total_obtain_mark,(
SELECT GROUP_CONCAT( distinct total_obtain_mark ORDER BY total_obtain_mark DESC )
FROM exam_results WHERE school_id = $school_id AND exam_id = $exam_id AND class_id = $class_id AND section_id = $section_id ))
AS rank
FROM exam_results
WHERE school_id = $school_id AND exam_id = $exam_id AND class_id = $class_id AND section_id = $section_id AND total_obtain_mark = $mark";

否则,如果您有一些具有相同排名的学生,您将跳过位置

关于php - 数据未正确来自数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57134317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com