[英]Mysql - Pass Variable to Subquery From Clause
I have the following query that I am working on, you'll find sample data 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;
This code works perfect (thanks to Tobias Thornfeldt Nissen) but what I'm now trying to do is make the player id (785) dynamic... I can't seem to pass my id variable (cc6_MensLeague_players.id) to the subquery from clause. 这段代码非常完美(由于Tobias Thornfeldt Nissen),但是我现在想做的是使玩家ID(785)动态...我似乎无法将ID变量(cc6_MensLeague_players.id)传递给子查询from子句。
I have tried the following methods: 我尝试了以下方法:
Adding @id :=
to the beginning of ... cc6_mensleague_players.id AS id ...
and changing ... WHERE player = 785 ...
to ... WHERE player = @id ...
NO LUCK! 将@id :=
添加到... cc6_mensleague_players.id AS id ...
的开头,然后将... WHERE player = 785 ...
更改为... WHERE player = @id ...
没有运气!
Changing ... player = 785 ORDER BY DATE ASC ...
to ... player = @id GROUP BY ID ORDER BY DATE ASC ...
NO LUCK! 将... player = 785 ORDER BY DATE ASC ...
更改为... player = @id GROUP BY ID ORDER BY DATE ASC ...
不走运!
I have also tried a few other tactics but I just can't figure it out. 我还尝试了其他一些策略,但我无法弄清楚。
So my question is: How do I make WHERE player = cc6_mensleague_players.id
(line 9) work? 所以我的问题是:如何使WHERE player = cc6_mensleague_players.id
(第9行)正常工作?
If you can point me in the right direction it would be greatly appreciated. 如果您能指出正确的方向,将不胜感激。
Thanks. 谢谢。
you can use the alias that you gave to the subquery as id
: 您可以使用给子查询提供的别名作为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;
Thanks for your replies :-) 感谢您的回复:-)
I was able to figure out the solution. 我能够找出解决方案。 Couldn't have done it without all of you. 没有你们所有人都做不到。
I ended up removing the brackets surrounding the 2nd from clause 我最终删除了第2个from子句周围的括号
... FROM (SELECT *
... ) AS temp
... ... FROM (SELECT *
... ) AS temp
...
and voila - The @id variable worked like a charm 和瞧-@id变量就像一个魅力一样工作
Here is the final piece of code 这是最后的代码
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
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;
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
cc6_MensLeague_players 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 https://www.db-fiddle.com/f/35EQE2yzdxNs3KHZnQkMuQ/6
Thanks Everyone. 感谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.