[英]what does SQL @x := @x +1 and @x := 0 mean?
I was doing rank score problem at leetcode and I am not sure below solution. 我在leetcode上进行排名得分问题,不确定在解决方案以下。 I can understand every part except for
@x := @x +1
and @x := 0
. 我可以理解除
@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;
Anyone could help please? 有人可以帮忙吗?
declare a var @x
initialization it as int 0
声明一个var
@x
初始化为int 0
select @x := 0
When you doing select
clause the @x
will add 1
. 当你在做
select
子句@x
将增加1
。
select @x := @x +1
Here is a sample 这是一个样本
Schema (MySQL v5.6) 模式(MySQL v5.6)
CREATE TABLE T(
col1 varchar(51)
);
INSERT INTO T VALUES ('TEST');
INSERT INTO T VALUES ('TEST1');
Query #1 查询#1
SELECT *,@x:=@x +1
FROM T CROSS JOIN (select @x := 0) v;
| col1 | @x := 0 | @x:=@x +1 |
| ----- | ------- | --------- |
| TEST | 0 | 1 |
| TEST1 | 0 | 2 |
NOTE 注意
select score, @x := @x +1 as rank
from (select distinct score from scores order by score desc) s, (select @x := 0) r
,
comma between two tables in the query means CROSS JOIN
,
逗号表示CROSS JOIN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.