2016-03-16 81 views
0

首先我有這個返回的所有足球比賽的日期,其中的MySQL - 顯示兩個內單獨的列中加入

HomeShotsOnTarget(HST)= FullTimeHomeGoals(FTHG)

AwayShotsOnTarget(AST)= FullTimeAWayGoals(FTHG)

SELECT MatchDate, HomeTeam, AwayTeam 
FROM matches 
WHERE HST=FTHG or AST=FTAG 

這將顯示

MatchDate | HomeTeam | AwayTeam 
2003/08/23 17   32 
2003/09/13 24   39 

等等等等...

HomeTeam和AwayTeam下的數字是在所謂的俱樂部另一個表裏面也有球隊實名TeamCodes。

以下內容與表格俱樂部中RealName的HomeCam匹配。

SELECT MatchDate, RealName 
FROM club T1 
INNER JOIN matches T2 ON T1.TeamCode = T2.HomeTeam 

這顯示

MatchDate| RealName| 
2003/08/23 Arsenal 
2003/09/13 Blackburn 

等...

所以我的問題是,我似乎無法找到顯示在HomeTeam實名和AwayTeam代替TeamCode的方式。像這樣...

MatchDate | HomeTeam | AwayTeam 
2003/08/23 Arsenal Aston Villa 
2003/09/13 Blackburn Man Utd 

回答

0

你只需要兩次加入我們的團隊,表,嘗試此查詢:

SELECT 
    MatchDate, 
    T1.RealName, 
    T2.RealName 
FROM 
    matches INNER JOIN club T1 ON (matches.HomeTeam = T1.TeamCode) 
      INNER JOIN club T2 ON (matches.AwayTeam = T2.TeamCode) 
WHERE 
    HST=FTHG OR AST=FTAG 
+0

謝謝你的迴應! 一個小問題: 當我做 SELECT MatchDate,HomeTeam,AwayTeam從匹配WHERE HST = FTHG或AST = FTAG 返回315行,當我做你的返回435行。 這可能與WHERE子句有關嗎? –

+0

這是奇怪的。俱樂部桌上的TeamCode欄中是否有重複的內容? –

+0

不,沒有,TeamCode 1到72沒有重複:/我認爲這很奇怪 –

1

也許是這樣的:

SELECT MatchDate, homeTeam.RealName AS HomeTeam, awayTeam.RealName AS AwayTeam 
FROM matches m 
INNER JOIN club homeTeam ON (m.HomeTeam = homeTeam.TeamCode) 
INNER JOIN club awayTeam ON (m.AwayTeam = awayTeam.TeamCode); 

我用它來把一些意義的標籤,而不是隻,T1T2

0

你只需要做的另一加入俱樂部表爲客隊首開紀錄,就像這樣:

SELECT MatchDate, T1.RealName AS HomeTeamName, T3.RealName AS AwayTeamName 
FROM club T1 
INNER JOIN matches T2 ON T1.TeamCode = T2.HomeTeam 
INNER JOIN club T3 ON T3.TeamCode = T2.AwayTeam 
相關問題