【问题标题】:Data not Coming correctly from database数据未正确来自数据库
【发布时间】:2019-07-21 14:54:55
【问题描述】:

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

我的数据库

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 '--'; 
    }
}

}

【问题讨论】:

  • 你有没有重复的考试成绩围绕着缺失等级的案例?
  • 是的,怎么解决?

标签: php mysql sql codeigniter


【解决方案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";

否则,如果你有一些相同等级的学生,你会跳过位置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多