在mySQL中显示结果的数字索引顺序作为附加列

时间:2013-10-09 14:57:08

标签: mysql sql-order-by

首先q永远啊!

我有一个带数据的mySQL表

**username**   **score**   
Count Joe         278     
Count Joe         255
Count Joe         298
Count Joe         289

我在显示以下结果集时遇到问题:

**RANK** **USERNAME** **SCORE**
1 Count Joe 255
2 Count Joe 278
3 Count Joe 289
4 Count Joe 298

目前正在返回

**RANK** **USERNAME** **SCORE**
    1 Count Joe 255
    22 Count Joe 278
    32 Count Joe 289
    18 Count Joe 298

使用

SET @ROW=0; SELECT @ROW:=@ROW + 1 AS rank, username, total FROM game_scores GROUP BY username ORDER BY total ASC LIMIT 10

上面的查询似乎返回索引位置或查询表中的用户。

1 个答案:

答案 0 :(得分:0)

SELECT @curRow := @curRow + 1 AS rank, s1.*
FROM
(
    SELECT   username, score
    FROM     scores    
    GROUP BY username ORDER BY score ASC LIMIT 10
) AS s1
JOIN (SELECT @curRow := 0) r