我已經寫了一個SQL查詢來顯示特定團隊的時間表以及他們的分數和第一次下降。如何重寫SQL查詢以獲得一個球隊的統計數據與對手的統計數據?
SELECT DISTINCT
/*From statsSchedules*/
homeScore
/*From leagueTeams*/
, homeTeam.logoId homeLogoId
, homeTeam.abbrName homeAbbrName
/*From statsTeamStats*/
, homeTeamStats.off1stDowns AS home1stDowns
/*From statsSchedules*/
, awayScore
/*From leagueTeams*/
, awayTeam.logoId awayLogoId
, awayTeam.abbrName awayAbbrName
/*From statsTeamStats*/
, awayTeamStats.off1stDowns AS away1stDowns
FROM statsSchedules
INNER JOIN leagueTeams AS homeTeam
ON statsSchedules.homeTeamId = homeTeam.TeamId
INNER JOIN statsTeamStats AS homeTeamStats
ON statsSchedules.homeTeamId=homeTeamStats.teamId
AND statsSchedules.homeTeamId=homeTeamStats.teamId
AND statsSchedules.scheduleId=homeTeamStats.scheduleId
INNER JOIN leagueTeams AS awayTeam
ON statsSchedules.awayTeamId = awayTeam.teamId
INNER JOIN statsTeamStats AS awayTeamStats
ON statsSchedules.awayTeamId=awayTeamStats.teamId
AND statsSchedules.awayTeamId=awayTeamStats.teamId
AND statsSchedules.scheduleId=awayTeamStats.scheduleId
WHERE awayTeam.abbrName LIKE 'DAL'
AND statsSchedules.stageIndex=1 OR homeTeam.abbrName LIKE 'DAL'
AND statsSchedules.stageIndex=1
ORDER BY `statsSchedules`.`weekIndex` ASC
該查詢輸出這個..
|homeScore|homeLogoId|homeAbbrName|home1stDowns|awayScore|awayLogoId|awayAbbrName|away1stDowns|
| 24 | 10 | DAL | 12 | 16 | 15 | NYG | 8 |
| 10 | 3 | DEN | 8 | 16 | 10 | DAL | 11 |
| 16 | 6 | ARI | 16 | 22 | 10 | DAL | 9 |
| 34 | 10 | DAL | 12 | 13 | 23 | LAR | 7 |
| 13 | 10 | DAL | 11 | 31 | 19 | GB | 8 |
我想現在要做的是創建一個具有DAL的總得分和對手DAL拿下總積分方面發揮了新表。
我還想要一個顯示DAL的1stDowns的列和另一個顯示其對手所造成的第一個下降總數的列。
預計
|mainTeamPts|mainTeam1stDowns|opponentsPts|opponents1stDowns|
| 109 | 55 | 86 | 59 |
UPADTE你的問題,顯示你需要 – scaisEdge
結果你是不是清楚什麼表,你wnat外觀喜歡。請閱讀[mcve]並採取行動(正如您在提問中所述)。對於其中的任何一個,你有什麼目標?閱讀關於SUM。 – philipxy
好吧我已編輯我的問題,並添加了預期。對不起,我還不熟悉SQL,因此想弄清楚如何正確提問有點困難。 – ssx95351