mysql查询评级系统

时间:2012-07-04 08:50:39

标签: mysql ranking rating

select 
    user_id,
    @pos:=(@pos+1) as new_position,
    (coins+total_item_costs) as wealth 
from user_ledger 
join users using (user_id),(select @pos:=0) p 
ORDER BY wealth DESC 
limit 10;

+---------+--------------+------------+
| user_id | new_position | wealth     |
+---------+--------------+------------+
|      19 |           19 | 1112823871 |
|      11 |           11 |   13318047 |
|       8 |            8 |    7292407 |
|       6 |            6 |    6122746 |
|      27 |           27 |    5271889 |
|      23 |           23 |    5263050 |
|       9 |            9 |    5171734 |
|       3 |            3 |    5136092 |
|      15 |           15 |    5097488 |
|       4 |            4 |    5089487 |
+---------+--------------+------------+
10 rows in set (0.01 sec)

new_position不正确.. 怎么了,伙计们? :)

PS。请不要告诉我使用临时表

1 个答案:

答案 0 :(得分:0)

事先应该在ORDER BY处进行职位。

SELECT user_id,@pos:=(@pos+1) as new_position,wealth FROM (
      select user_id,(coins+total_item_costs) as wealth from user_ledger join users 
      using (user_id) ORDER BY wealth DESC limit 10 ) a,(select @pos:=0) p