排名基于两列

时间:2014-10-28 16:52:17

标签: mysql sql database

我需要有关mysql查询的帮助。查询必须能够显示排名,如图http://s27.postimg.org/loixkzipv/img.png

如果胜利相同,则与“差异”列进行比较,维护列表顺序。请帮助解决此问题。谢谢。

我试过了:

 SELECT player, wins, diff, rank 
 FROM (
     SELECT player, wins, diff, 
         @curRank := IF(@prevRank = diff, @curRank, @incRank) AS rank, 
         @incRank := @incRank + 1, 
         @prevRank := diff 
     FROM tmpPoradi p, (SELECT @curRank := 0, @prevRank := NULL, @incRank := 1) r  
     ORDER BY diff 
     DESC) s;

但是这个函数只比较diff,并且不引用wins。我如何让它也考虑到胜利?

1 个答案:

答案 0 :(得分:2)

我认为以下查询将获得您需要的结果。首先,你必须计算子查询中的排名。我认为比你的方法简单得多,你只需要按胜利和差异按降序排序结果(假设字段是数字)。然后你只需选择所有结果并改变命令玩家desc。

 SELECT player, wins, diff,rank from
 (
 SELECT player, wins, diff, @winrank := @winrank + 1 AS rank
 from tmpPoradi,(SELECT @winrank := 0) r 
 ORDER BY wins DESC,diff DESC
 )  rt
ORDER BY player

希望我没有遗漏任何东西。