php - How to get the rank of a specific user in a particular subject for all the subjects scored acknowledging ties -
--------------------------------------------------------------------- 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 have query gets of particular students results follow
select * results stid='13' , year='2014/2015' order subject asc limit 20
and have 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 aim particular students rank in each subject , have sub query inside while loop of first sql statement follow
select rank_number, id, name, total_cumulative ( select id, name, total_cumulative, @rank:=@rank+1 rank_number (select pl.id, pl.name,sum(en.total_cumulative) total_cumulative students pl join results en on pl.id = en.stid en.subject = '$subject' , en.year='$year' group pl.id order total_cumulative desc ) rankings, (select @rank:=0) r ) overall_rankings id = '13' limit 0, 1";
after have 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 want query check ties , give results in 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 me newbies in mysql programming
Comments
Post a Comment