我可以对此查询使用一些帮助。
这是我的表:
id name point
1 yogi 5
2 sasi 10
3 white 5
4 black 8
我的查询是:
SELECT * FROM points ORDER BY point DESC
结果是:
id name point
2 sasi 10
4 black 8
1 yogi 5
3 white 5
但我想得到如下结果。请指导我。
id name point rank
2 sasi 10 1
4 black 8 2
1 yogi 5 3
3 white 5 4
这可能吗?
提前感谢。
答案 0 :(得分:1)
试试这个
SET @rank = 0;
SELECT *, @rank:=@rank+1 AS rank
FROM points ORDER BY point DESC
作为单行查询,如下面的
SELECT *, @rank:=@rank+1 AS rank
FROM points, (SELECT @rank:=0) temp
ORDER BY point DESC
答案 1 :(得分:0)
SELECT
*,
@rank:=@rank+1 'rank' FROM points, (select @rank:=0) r
ORDER BY
point DESC