1 2 3 4 5 6 7 8 9 10 11
| select d.Score,e.Rank from Scores d join (select b.*,@num:=@num+1 as "Rank" from (select * from (select Score from Scores group by Score) a order by a.Score desc) b,(select @num:=0) c) e on d.Score=e.Score order by d.Score desc; 或者 select d.Score,e.Rank from Scores d join (select b.*,@num:=@num+1 as "Rank" from (select distinct Score from Scores order by Score desc) b,(select @num:=0) c) e on d.Score=e.Score order by d.Score desc;
解题思路: 1. 去重排序子表 select distinct Score from Scores order by Score desc 2. 对子表添加排序 select b.*,@num:=@num+1 as "Rank" from (select distinct Score from Scores order by Score desc) b,(select @num:=0) c 3. join select d.Score,e.Rank from Scores d join (select b.*,@num:=@num+1 as "Rank" from (select distinct Score from Scores order by Score desc) b,(select @num:=0) c) e on d.Score=e.Score order by d.Score desc;
|