MySQL修改查询以获取特定行

时间:2018-02-21 06:13:25

标签: mysql

我有以下MySQL查询:

SELECT
    @rownum:=@rownum+1 rank,
    userID,
    xpTotal
from users xpTotal, (SELECT @rownum:=0) r
WHERE username != '' && bot = 'false'
ORDER BY xpTotal DESC

结果是这样的:

rank   |    userID    |  xpTotal 
--------------------------------
  1    |  2934729447  |   52873
  2    |  8523954935  |   33465
  3    |  4576456556  |   13466
  4    |  2341234555  |   04244
  5    |  3453565334  |   02297

如何修改我的查询以获得ID 2341234555的排名?意思是,在这种情况下,查询只会输出第4行。

1 个答案:

答案 0 :(得分:1)

您需要将当前包装在子查询中,否则如果在当前WHERE子句的条件中添加它,则排名始终为1.

SELECT *
FROM
(
    SELECT
        @rownum:=@rownum+1 rank,
        userID,
        xpTotal
    FROM users xpTotal, (SELECT @rownum:=0) r
    WHERE username != '' && bot = 'false'
) a
WHERE a.UserID = '2341234555';