我试图建立学生成绩管理系统。当我试图根据每个学生在考试中的总成绩而给每个学生分配职位时,我会遇到问题。在我的输出中,我得到第一(获得标记450),第二(获得标记449),第四(获得标记448)。第三位置丢失。 5、10和其他一些位置也缺少。但是我在代码中没有发现任何问题。
我的数据库
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 '--';
}
}
}
答案 0 :(得分:1)
您的子查询中需要不同的total_obtain_mark
GROUP_CONCAT( distinct total_obtain_mark ORDER BY total_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";
否则,如果您有一些同等级的学生,则跳过位置