How to get the rank of a specific user in a particular subject for all the subjects scored acknowledging ties

我只是一个虾纸丫 提交于 2019-12-29 09:45:30

问题


---------------------------------------------------------------------
   id   |   stid   |   Subject    |   Total_cumulative  | Year     | 
----------------------------------------------------------------------
    1   |     23   |  English     |    40               | 2014/2015
    2   |     1    |  English     |    29               | 2014/2015
    3   |     13   |  Maths       |    40               | 2014/2015
    4   |     4    |  Physics     |    60               | 2014/2015
    5   |     13   |  Commerce    |    40               | 2014/2015
    6   |     1    |  Biology     |    89               | 2014/2015
    7   |     13   |  English     |    29               | 2014/2015
    8   |     13   |  Agric       |    60               | 2014/2015

Now I have a query that gets all of a particular students results as follow

SELECT * FROM results where stid='13' AND year='2014/2015' 
ORDER BY subject ASC LIMIT 20

and I have the following result

---------------------------------------------------------------------
   id   |   stid   |   Subject    |   Total_cumulative  | Year     | 
----------------------------------------------------------------------
    3   |     13   |  Maths       |    40               | 2014/2015
    5   |     13   |  Commerce    |    40               | 2014/2015
    7   |     13   |  English     |    89               | 2014/2015
    8   |     13   |  Agric       |    60               | 2014/2015

But my aim is to get the particular students rank in each subject and I have a sub query inside my the while loop of my first sql statement as follow

SELECT rank_number, id, name, total_cumulative 
FROM ( SELECT id, name, total_cumulative, @rank:=@rank+1 AS rank_number FROM    
(SELECT pl.id, pl.name,SUM(en.total_cumulative) AS total_cumulative
FROM students pl JOIN results en ON pl.id = en.stid
WHERE en.subject = '$subject' AND en.year='$year' 
GROUP BY pl.id ORDER BY total_cumulative DESC ) AS rankings, 
(SELECT @rank:=0) AS r ) AS overall_rankings 
WHERE id = '13'
LIMIT 0, 1";

after that I have the following result

----------------------------------------------------------------------------
   id   |   stid   |   Subject    |   Total_cumulative  | Year       | Rank  
----------------------------------------------------------------------------
    3   |     13   |  Maths       |    40               | 2014/2015  | 1
    5   |     13   |  Commerce    |    40               | 2014/2015  | 2
    7   |     13   |  English     |    29               | 2014/2015  | 3
    8   |     13   |  Agric       |    60               | 2014/2015  | 1

but I want the query to check for ties and give the results in the following order

----------------------------------------------------------------------------
   id   |   stid   |   Subject    |   Total_cumulative  | Year       | Rank  
----------------------------------------------------------------------------
    3   |     13   |  Maths       |    40               | 2014/2015  | 1
    5   |     13   |  Commerce    |    40               | 2014/2015  | 2
    7   |     13   |  English     |    29               | 2014/2015  | 2
    8   |     13   |  Agric       |    60               | 2014/2015  | 1

Please help me I am a newbies in mysql programming

来源:https://stackoverflow.com/questions/29388723/how-to-get-the-rank-of-a-specific-user-in-a-particular-subject-for-all-the-subje

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!