mysql - How to get the ranks of two different colums in a table in my case? -
so have table named db_points.
---------------------------------------- | name | points | spoints | rpoints | ---------------------------------------- | max | 240 | 50 | 1242 | | alvin | 600 | 123 | 3012 | | amanda | 234 | 1000 | 132 | | angela | 50 | 514 | 4023 | | rudolph | 2000 | 230 | 1232 | ----------------------------------------
i need rank based on ordering tpoints (points+spoints), points, spoints, rank based on rpoints , rpoints.
this following angela, rudolph or amanda.
-------------------------------------------------------------------- | rankt | name | tpoints | points | spoints | rankr | rpoints | -------------------------------------------------------------------- | 4 | angela | 564 | 50 | 514 | 5 | 4023 | | 1 | rudolph | 2230 | 2000 | 230 | 2 | 1232 | | 2 | amanda | 1234 | 234 | 1000 | 1 | 132 | --------------------------------------------------------------------
as see rankings different. higher tpoints, better rankt; lower rpoints, better rankr.
however need in 1 sql-query. got far:
select rank, name, tpoints, points, spoints rankr (select (@pos := @pos+1) pos, (@rank := if(@prev = tpoints,@rank,@pos)) rank, tpoints, points, spoints, name, rpoints, select @rownum:=@rownum + 1 rankr,t.* (select (@prev := spoints+points) tpoints, spoints, points, name, rpoints db_points order tpoints desc) ll, (select rpoints, name db_points rpoints > 0 order rpoints asc) t,(select @rownum := 0) r) l name = '%s';
hope understandable.
so far have 2 working querys need put in one
select rank, name, tpoints, points, spoints (select (@pos := @pos+1) pos, (@rank := if(@prev = tpoints,@rank,@pos)) rank, tpoints, points, spoints, name (select (@prev := spoints+points) tpoints, spoints, points, name db_points order tpoints desc) ll) l name = '%s';
and 1
select rankr (select @rownum:=@rownum + 1 rankr,t.* (select rpoints, name db_points rpoints > 0 order rpoints asc) t,(select @rownum := 0) r) name='%s';
hope can read code, im bad in formating mysql code.
please give try
select @rankt:=@rankt+1 rankt, t.* (select name, (points+spoints) tpoints, points, spoints, @rankr := @rankr + 1 rankr, rpoints db_points p, (select @rankr:=0)initialr order rpoints asc )t,(select @rankt:=0)intialt order tpoints desc
then can put whole query subquery , outer select where name '%s'
Comments
Post a Comment