mysql顺序是一个非唯一的排名?

时间:2011-10-26 16:30:33

标签: mysql sql-order-by

  

可能重复:
  How do I Handle Ties When Ranking Results in MySQL?

我想按Status订购表格Activepoints,然后将排名插入到列rank中。 它来了:

RANK   USERNAME   ACTIVEPOINTS

 1     monkey100     92
 2     fresnoJump    54
 3     otherUser9    54

但它应该显示非唯一的排名,如

RANK   USERNAME   ACTIVEPOINTS

 1     monkey100     92
 2     fresnoJump    54
 2     otherUser9    54

1 个答案:

答案 0 :(得分:1)

我认为这是通过2个UDF变量http://dev.mysql.com/doc/refman/5.0/en/user-variables.html来解决的。

SET @rank = 0, @previous_active_points = 0;
SELECT IF(@previous_active_points<>Activepoints,@rank:=@rank+1,@rank) AS rank,
@previous_active_points:=Activepoints,username,Activepoints 
FROM `status` ORDER BY Activepoints

希望它有所帮助!