mysql在考试中获得学生排名

时间:2017-04-03 06:56:17

标签: php mysql

我有以下查询。

SELECT 
  SUM(
    marks_obtained_written + marks_obtained_oral + marks_obtained_classwork
  ),
  FIND_IN_SET(
    SUM(
      marks_obtained_written + marks_obtained_oral + marks_obtained_classwork
    ),
    (SELECT 
      GROUP_CONCAT(summarks 
        ORDER BY summarks DESC) 
    FROM
      (SELECT 
        SUM(
          marks_obtained_written + marks_obtained_oral + marks_obtained_classwork
        ) AS summarks 
      FROM
        results 
      GROUP BY student_id) sm)
  ) AS rank 
FROM
  results 
WHERE student_id = ".$student_id." 
  AND exam_year = ".$year." 
  AND exam_id = ".$exam." 
  AND results.class_id = ".$class_id." 
  AND section_id = ".$section_id." 

只有在我的结果表中进行一次考试时,此查询才适用于我。 当有更多考试时,此查询会计算所有考试的分数并将等级返回给我。但是如果我只需要一次考试的学生排名,我需要在查询中放入exam_id,当我这样做时,查询返回0作为学生排名。

enter image description here

在上面的图片中,我得到了第0个无效的位置。但是如果我删除了exam_id条件,那么我得到了正确的等级,但问题是所有输入的考试的总和标记,我不想要因为我需要排名特定的考试。

所以请任何人告诉我在哪里将exam_id条件放在我的查询中。

1 个答案:

答案 0 :(得分:1)

尝试将exam_id也放在子查询中。

(SELECT 
      GROUP_CONCAT(summarks 
        ORDER BY summarks DESC) 
    FROM
      (SELECT 
        SUM(
          marks_obtained_written + marks_obtained_oral + marks_obtained_classwork
        ) AS summarks 
      FROM
        results WHERE exam_id = ?
      GROUP BY student_id