2013-08-02 97 views
0

我一直在尋找這個,我總是得到答案,改變了做事的方式,但不解決一般問題,即:引用嵌套SELECT中的主要查詢變量。MySQL - 引用嵌套選擇主查詢(需要嵌套)

我需要它是一個嵌套查詢,因爲數據結構非常複雜。讓我們來看看,我需要做的例子:

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:[email protected]+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」 - 我試過並且無法做到這一點,所以我最終使用了session變量。

我從此查詢得到的是@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 | 
+------------+------------+----------+----------+--------+ 

EDIT 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:[email protected]+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' 

選擇病例後@rank使得它不爲0的兩倍,並且選擇之前將@rank復位。否則,它將變爲0,但進入每支球隊的第二位球員。

希望有人能找到有用的這個問題。

感謝Kordirko再次爲他的時間和答案。

回答

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:[email protected]+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) 
+0

你好Kordirko!非常感謝您的回答。我得到一個錯誤,我通過將所有表連接到主表(「er_actions」)解決了這個問題,因爲player.team_id不能被引用,因爲它是第一個表而不是JOINED表。它現在正在工作,但我不明白爲什麼,當球隊不同時,計數器不會變爲0。也許你可以找到爲什麼,因爲我無法做到這一點 - 我認爲它應該,正如你所說的那樣。再次感謝您的時間和答案! – Unapedra

+0

我已經找到了合適的解決方案,這要感謝您的參與以及我的參與。非常感謝您的時間和幫助!沒有你的答案,我不會做到的! – Unapedra