我有一个非常简单的
MySQL表,我用admin_no为每个学生保存考试和CA分数的科目.它看起来像这样:
admin_no subject ca exam year class_s 10/00182 IRS 39 56 2014/2015 Grade 2 10/00177 English 39 59 2014/2015 Grade 2 10/00177 Mathematics 34 59 2014/2015 Grade 2 10/00177 Basic 37 59 2014/2015 Grade 2 10/00177 Social 39 60 2014/2015 Grade 2 10/00177 Yoruba 33 59 2014/2015 Grade 2 09/00159 English 37 59 2014/2015 Grade 2 09/00159 Mathematics 35 60 2014/2015 Grade 2 09/00159 Basic 39 59 2014/2015 Grade 2 .......................................................
我用下面的mySQL查询求和所有科目的考试分数和CA,然后将每个学生的所有分数总计为total_scores,然后对总分进行排名:
SELECT admin_no,rank,total_score FROM (SELECT *,IF(@marks=(@marks:=total_score),@auto,@auto:=@auto+1) AS rank FROM (SELECT * FROM (SELECT admin_no,SUM(exam)+SUM(ca) AS total_score,year,class_s FROM subjects_1,(SELECT @auto:=0,@marks:=0) as init WHERE `class_s`='Grade 2' and `year`='2014/2015' GROUP BY admin_no ) sub ORDER BY total_score DESC)t) as result
admin_no rank total_score 08/00076 1 1615 10/00170 2 1613 12/00300 3 1609 09/00091 4 1604 10/00182 5 1600 09/00159 6 1583 10/00177 7 1574 09/00152 8 1561 09/00165 9 1540 10/00176 10 1516 13/00354 11 1497 10/00178 12 1470 14/00348 13 1409 **14/00346 14 12 15/00371 14 12 09/00156 15 7**
问题:输出很好,但是在最后三个级别中都有问题.总分12在14/00346和15/00371中出现两次并且它们被给予相同的等级,这是好的但是接下来的12是7和它排名15而不是17.请帮助我,如果他们在total_scores中的关系,我不希望排名是连续的.
Bellow是我的MysqL数据的副本
http://youth-arena.com/portal/sql.sql
试试这个.
原文链接:https://www.f2er.com/php/136875.html我没有在那里包含year和class_s,因为你可能无法获得可靠的行集(因为你是admin_no的GROUPing).
我已经包括了序数和竞争排名 – 选择你需要的任何一个.
更多信息:https://en.wikipedia.org/wiki/Ranking
我不知道谁在并列时获得更高级别的指导方针,但作为一个建议 – 你可以做一个学生研究的主题数量来确定这个(包括在sql中用于举例目的).
SET @prev_value = NULL; SET @rank_count = 0; SET @rank_increasing = 0; SELECT @rank_increasing := @rank_increasing + 1 AS ordinal_rank,CASE WHEN @prev_value = a.total_score THEN @rank_count WHEN @prev_value := a.total_score THEN @rank_count := @rank_increasing END AS competition_rank,a.* FROM ( SELECT admin_no,SUM(exam) + SUM(ca) AS total_score,SUM(exam) AS sum_exam,SUM(ca) AS sum_ca,COUNT(DISTINCT subject) AS subject_count FROM subjects_1 GROUP BY admin_no ORDER BY total_score DESC ) a
截图摘要