2017-10-19 76 views
0

我已經寫了一個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   | 
+2

UPADTE你的問題,顯示你需要 – scaisEdge

+1

結果你是不是清楚什麼表,你wnat外觀喜歡。請閱讀[mcve]並採取行動(正如您在提問中所述)。對於其中的任何一個,你有什麼目標?閱讀關於SUM。 – philipxy

+0

好吧我已編輯我的問題,並添加了預期。對不起,我還不熟悉SQL,因此想弄清楚如何正確提問有點困難。 – ssx95351

回答

0

下面是基於小結果表上面的兩個例子,他們可能給你如何進行的想法。

在這裏展示:SQL Fiddle

的MySQL 5.6架構設置

CREATE TABLE QryResult 
    (`homeScore` int, `homeLogoId` int, `homeAbbrName` varchar(3), `home1stDowns` int, `awayScore` int, `awayLogoId` int, `awayAbbrName` varchar(3), `away1stDowns` int) 
; 

INSERT INTO QryResult 
    (`homeScore`, `homeLogoId`, `homeAbbrName`, `home1stDowns`, `awayScore`, `awayLogoId`, `awayAbbrName`, `away1stDowns`) 
VALUES 
    (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) 
; 

查詢1

select 
    teams.AbbrName, r1.homeScore, r1.home1stDowns, r2.awayScore, r2.away1stDowns 
from (
     select homeAbbrName AbbrName from QryResult 
     union 
     select awayAbbrName from QryResult 
    ) teams 
left join (
      select homeAbbrName, sum(homeScore) homeScore, sum(home1stDowns) home1stDowns 
      from QryResult 
      group by homeAbbrName 
     ) r1 on teams.AbbrName = homeAbbrName 
left join (
      select awayAbbrName, sum(awayScore) awayScore, sum(away1stDowns) away1stDowns 
      from QryResult 
      group by awayAbbrName 
     ) r2 on teams.AbbrName = awayAbbrName 
order by teams.AbbrName, homeAbbrName 

Results

| AbbrName | homeScore | home1stDowns | awayScore | away1stDowns | 
|----------|-----------|--------------|-----------|--------------| 
|  ARI |  16 |   16 | (null) |  (null) | 
|  DAL |  71 |   35 |  38 |   20 | 
|  DEN |  10 |   8 | (null) |  (null) | 
|  GB | (null) |  (null) |  31 |   8 | 
|  LAR | (null) |  (null) |  13 |   7 | 
|  NYG | (null) |  (null) |  16 |   8 | 

查詢2

select 
    abbrname 
    , logoid 
    , sum(score) score 
    , sum(1stdowns) 1stdowns 
from (
     select `homeScore` score, `homeLogoId` logoid, `homeAbbrName` abbrname, `home1stDowns` 1stdowns 
     from QryResult 
     union all 
     select `awayScore`, `awayLogoId`, `awayAbbrName`, `away1stDowns` 
     from QryResult 
    ) d 
GROUP BY 
    abbrname 
    , logoid 
ORDER BY 
    abbrname 
    , logoid 

Results

| abbrname | logoid | score | 1stdowns | 
|----------|--------|-------|----------| 
|  ARI |  6 | 16 |  16 | 
|  DAL |  10 | 109 |  55 | 
|  DEN |  3 | 10 |  8 | 
|  GB |  19 | 31 |  8 | 
|  LAR |  23 | 13 |  7 | 
|  NYG |  15 | 16 |  8 | 
+0

你的問題現在解決了嗎?你仍然有關於這個答案的問題嗎?要接受答案「[**點擊Tick **](https://ibb.co/ikqyO6)」以獲取更多信息,請參閱[help/accepting](https://stackoverflow.com/help/someone-answers) –