我在leetcode做排名得分问题,我不确定下面的解决方案。我能理解每一部分除了 @x := @x +1
以及 @x := 0
.
select scores.score, ranks.rank from scores left join (
select score, @x := @x +1 as rank from (select distinct score from scores order by score desc) s, (select @x := 0) r
)
as ranks on scores.score = ranks.score order by scores.score desc;
有人能帮忙吗?
1条答案
按热度按时间hs1rzwqc1#
声明变量
@x
初始化为int0
```select @x := 0
select @x := @x +1
CREATE TABLE T(
col1 varchar(51)
);
INSERT INTO T VALUES ('TEST');
INSERT INTO T VALUES ('TEST1');
SELECT *,@x:=@x +1
FROM T CROSS JOIN (select @x := 0) v;
select score, @x := @x +1 as rank
from (select distinct score from scores order by score desc) s, (select @x := 0) r