数据未正确来自数据库
Posted
技术标签:
【中文标题】数据未正确来自数据库【英文标题】: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 '--';
【问题讨论】:
你有没有重复的考试成绩围绕着缺失等级的案例? 是的,怎么解决? 【参考方案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";
否则,如果你有一些相同等级的学生,你会跳过位置
【讨论】:
以上是关于数据未正确来自数据库的主要内容,如果未能解决你的问题,请参考以下文章