0
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。一時テーブルを使用するように言わないでください

4

1 に答える 1

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
于 2012-07-04T09:02:33.653 に答える