我一直在寻找这个,我总是得到改变做事方式的答案,但是没有解决一般性问题,即:在嵌套的SELECT中引用主查询变量。
我需要它作为嵌套查询,因为数据结构非常复杂。让我们看看我需要做的例子:
SET @teamg_id := 0;
SELECT
@teamg_id := player.team_id,
player.team_id AS player_team,
event.local_team_id AS local_team,
event.visitor_team_id AS visitor_team,
action.unique_id AS action_id,
action.player_id AS player_id,
player.name AS player_name,
playerindex.rank AS player_pos,
playerindex.ses_var_nest AS ses_var_nest
FROM
er_players player,
er_events event,
er_actions action
LEFT JOIN
(SELECT
unique_id, team_id, @pn:=@pn+1 AS rank, @teamg_id AS ses_var_nest
FROM
er_players, (SELECT @pn:=-1) p2
WHERE
team_id = @teamg_id
ORDER BY
dorsal ASC, id DESC) playerindex
ON
playerindex.unique_id = action.player_id
WHERE
action.unique_id = '1374572622'
AND player.unique_id = action.player_id
AND event.unique_id = (SELECT event_id
FROM er_analysis
WHERE unique_id = action.analysis_id)
正如你所看到的,我需要在他的团队中返回此玩家的“索引”,按照返回的顺序返回查询(等级值),但我需要将其计算为与团队相关,而不是整个玩家表。
为了做到这一点,我需要在嵌套的SELECT中引用主查询值“player.team_id” - 我尝试过但无法做到,所以我结束了使用会话变量。
我从这个查询得到的是@teamg_id将值很好地存储在主查询中,但是当传递给嵌套查询时,它的值变为NULL:
@teamg_id | player_team | local_team | visitor_team | action_id | player_id | player_name | player_pos | ses_var_nest
1374570040 | 1374570040 | 1374570040 | 1374571827 | 1374572622 | 1374570146 | Player name | NULL | NULL
我的问题是如何在嵌套查询中使用会话变量(我认为应该使用它),或者如何在其中引用主查询值。
感谢您的帮助!
编辑:这是er_players表的结构,这是给我问题的那个。如果我说“给我'玩家3'的索引”它应该给我2(0, 1,2),因为这是他队伍中的第三位球员。
+------------+------------+----------+----------+--------+
| unique_id | team_id | name | position | dorsal |
+------------+------------+----------+----------+--------+
| 1374570066 | 1374570040 | Player 1 | Defense | 1 |
| 1374570034 | 1374570040 | Player 2 | Defense | 2 |
| 1374677119 | 1374571827 | Player 7 | Position | 7 |
| 1374570146 | 1374570040 | Player 3 | Attack | 5 |
+------------+------------+----------+----------+--------+
编辑2(解决方案):感谢Kordirko的回答和一些游戏,找到了解决方案。
SELECT
player.team_id AS player_team,
event.local_team_id AS local_team,
event.visitor_team_id AS visitor_team,
action.unique_id AS action_id,
action.player_id AS player_id,
player.name AS player_name,
playerindex.rank AS player_pos,
playerindex.ses_var_nest AS ses_var_nest
FROM
er_actions action
LEFT JOIN er_events event
ON event.unique_id = (SELECT event_id FROM er_analysis WHERE unique_id = action.analysis_id)
LEFT JOIN er_players player
ON player.unique_id = action.player_id
LEFT JOIN
(SELECT
unique_id, team_id, team_id AS ses_var_nest ,
case when team_id <> @lastteam
then (@pn:=-1)
end AS hander,
case when team_id <> @lastteam
then(@lastteam:=team_id)
end AS team_id_new,
@pn:=@pn+1 AS rank
FROM
er_players, (SELECT (@pn:=-1),(@lastteam:=0)) p2
ORDER BY
team_id, dorsal ASC, id DESC) playerindex
ON
playerindex.unique_id = action.player_id
AND playerindex.team_id = player.team_id /* new join condition */
WHERE
action.unique_id = '1374572622'
在CASES之后选择@rank使其不是0两次,并在选择之前重置@rank。否则,它会变成0但进入每个队伍的第二个球员。
希望有人能找到有用的问题。
再次感谢Kordirko的时间和答案。
答案 0 :(得分:1)
这不起作用。
SELECT子句总是在执行链的末尾进行评估,
@teamg_id变量在嵌套子查询中始终为0。
尝试这个查询,它没有经过测试(我无法看到你的数据,播放的样本数据总是有帮助的),但可能会给出预期的结果。
我注释掉了一些部分并添加了一个连接条件。
/* SET @teamg_id := 0; */
SELECT
/* @teamg_id := player.team_id, */
player.team_id AS player_team,
event.local_team_id AS local_team,
event.visitor_team_id AS visitor_team,
action.unique_id AS action_id,
action.player_id AS player_id,
player.name AS player_name,
playerindex.rank AS player_pos,
playerindex.ses_var_nest AS ses_var_nest
FROM
er_players player,
er_events event,
er_actions action
LEFT JOIN
(SELECT
unique_id, team_id, @pn:=@pn+1 AS rank, team_id AS ses_var_nest ,
case when team_id <> @lastteam
then (@pn:=-1) <> (@lastteam:=team_id) end
FROM
er_players, (SELECT (@pn:=-1),(@lastteam:=0)) p2
/* WHERE
team_id = @teamg_id */
ORDER BY
team_id, dorsal ASC, id DESC) playerindex
ON
playerindex.unique_id = action.player_id
AND playerindex.team_id = player.team_id /* new join condition */
WHERE
action.unique_id = '1374572622'
AND player.unique_id = action.player_id
AND event.unique_id = (SELECT event_id
FROM er_analysis
WHERE unique_id = action.analysis_id)