我正在处理以下查询,您会找到示例数据 on this db fiddle :
SET @count=0;
SELECT ( @rank := IF(@points = points, @rank + 1, IF(@points := points, @rank + 1, @rank + 1)) ) AS rank,
er.*
FROM (SELECT cc6_MensLeague_players.id AS `id`,
( 1288 ) AS points,
(SELECT Max(@count := IF(outcome = 2, @count + 1, 0)) AS max_consecutive_wins
FROM (SELECT *
FROM cc6_MensLeague_rounds
WHERE player = 785
ORDER BY date ASC) AS temp
) AS `maxcon`
FROM cc6_MensLeague_players
GROUP BY `id`) er
CROSS JOIN (SELECT @rank := 0, @points := -1) params
ORDER BY id DESC;
此代码运行完美(由于Tobias Thornfeldt Nissen),但是我现在想做的是使玩家ID(785)动态...我似乎无法传递我的ID变量(cc6_MensLeague_players.id)到子查询from子句。
我尝试了以下方法:
在@id :=
的开头添加... cc6_mensleague_players.id AS id ...
,并将... WHERE player = 785 ...
更改为... WHERE player = @id ...
,祝您好运!
将... player = 785 ORDER BY DATE ASC ...
更改为... player = @id GROUP BY ID ORDER BY DATE ASC ...
真幸运!
我也尝试了其他一些战术,但我只是想不通。
所以我的问题是:
如何使WHERE player = cc6_mensleague_players.id
(第9行)正常工作?
如果您能指出正确的方向,将不胜感激。
谢谢。
答案 0 :(得分:0)
您可以将为子查询提供的别名用作id
:
SET @count=0;
SELECT ( @rank := IF(@points = points, @rank + 1,
IF(
@points := points,
@rank + 1,
@rank + 1)) )
AS rank,
er.*
FROM (SELECT cc6_MensLeague_players.id
AS
`id`,
( 1288 )
AS
points,
(SELECT Max(@count := IF(
outcome = 2,
@count + 1,
0)) AS
max_consecutive_wins
FROM (SELECT *
FROM
cc6_MensLeague_rounds
WHERE player = `id`
ORDER BY date ASC) AS
temp) AS
`maxcon`
FROM cc6_MensLeague_players
GROUP BY `id`) er
CROSS JOIN (SELECT @rank := 0,
@points := -1) params
ORDER BY id DESC;
答案 1 :(得分:0)
感谢您的回复:-)
我能够找出解决方案。 没有你们所有人都做不到。
我最终从条款中删除了第二个括号
... FROM (SELECT *
... ) AS temp
...
和瞧-@id变量的作用就像一个魅力
这是最后的代码
set @id=0, @count=0;
select ( @rank := if(@points = points, @rank + 1, if(@points := points, @rank + 1, @rank + 1)) ) as rank,
er.*
from (select @id := cc6_MensLeague_players.id as
id ,
( 1288 ) as points,
(select max(@count := if(outcome = 2, @count + 1, 0)) as MaxWins
from cc6_MensLeague_rounds
where player = @id
order by date asc
) as
maxcon
from cc6_MensLeague_players
group by
id ) er
cross join (select @rank := 0, @points := -1) params
order by id desc;
https://www.db-fiddle.com/f/35EQE2yzdxNs3KHZnQkMuQ/6
谢谢大家。