2015-12-12 66 views
0

我有代碼爲主客場結果在單獨的查詢,但我不確定如何加入這一起來獲取主客場。加入主客場比賽的結果在體育表

碼網站首頁體育聯賽

SELECT 
teamName AS Team, 
Sum(P) AS P, 
Sum(W) AS W, 
Sum(D) AS D, 
Sum(L) AS L, 
SUM(F) as F, 
SUM(A) AS A, 
SUM(GD) AS GD, 
SUM(Pts) AS Pts 
    FROM(
    SELECT 
    teamHomeID Team, 
    1 P, 
    IF(matchScoreHome > matchScoreAway,1,0) W, 
    IF(matchScoreHome = matchScoreAway,1,0) D, 
    IF(matchScoreHome < matchScoreAway,1,0) L, 
    matchScoreHome F, 
    matchScoreAway A, 
    matchScoreHome-matchScoreAway GD, 
    CASE WHEN matchScoreHome > matchScoreAway THEN 3 WHEN matchScoreHome = matchScoreAway THEN 1 ELSE 0 END PTS 
FROM tblmatches WHERE matchstatus = 'FT' AND competitionID = 1 AND seasonID = 2  
) as tot 
JOIN tblteam t ON tot.team=t.teamID 
GROUP BY Team 
ORDER BY SUM(Pts) DESC ; 

CODE AWAY體育聯賽

SELECT 
teamName AS Team, 
Sum(P) AS P, 
Sum(W) AS W, 
Sum(D) AS D, 
Sum(L) AS L, 
SUM(F) as F, 
SUM(A) AS A, 
SUM(GD) AS GD, 
SUM(Pts) AS Pts 
FROM(
SELECT 
teamAwayID Team, 
    1 P, 
    IF(matchScoreHome < matchScoreAway,1,0) W, 
    IF(matchScoreHome= matchScoreAway,1,0) D, 
    IF(matchScoreHome > matchScoreAway,1,0) L, 
    matchScoreAway F, 
    matchScoreHome A, 
    matchScoreAway-matchScoreHome GD, 
CASE WHEN matchScoreHome < matchScoreAway THEN 3 WHEN matchScoreHome = matchScoreAway THEN 1 ELSE 0 END PTS 
FROM tblmatches WHERE matchstatus = 'FT' AND competitionID = 1 AND seasonID = 2 
) as tot 
JOIN tblteam t ON tot.team=t.teamID 
GROUP BY Team 
ORDER BY SUM(Pts) DESC ; 

每個表將類似於遵循

-------------------------------------------------- 
¦ Team Name ¦ P ¦ W ¦ D ¦ L ¦ F ¦ A ¦ GD ¦ Pts ¦ 
¦ Man City ¦ 8 ¦ 6 ¦ 0 ¦ 2 ¦ 23 ¦ 10 ¦ 13 ¦ 18 ¦ 
¦ Tottehham ¦ 8 ¦ 4 ¦ 4 ¦ 0 ¦ 14 ¦ 5 ¦ 9 ¦ 16 ¦ 
-------------------------------------------------- 

我想這兩個表聯合起來得到的東西如下所示:

 ----------------------------------------------------------------------------- 
Team Name ¦ P ¦ W ¦ D ¦ L ¦ F ¦ A ¦ GD ¦ HW ¦ HD ¦ HL ¦ HF ¦ HA ¦ AW ¦ AD ¦ AL ¦ PTS ¦ 

回答

0

快速解決方法是將'tot'重命名爲'tothome'和'totaway',然後您可以或多或少地合併查詢。然後,你將不得不參考例如F as totaway.F以獲得球隊在客場比賽中的進球。

而你會在SUM(P)中得到P的不明確性。在那裏你必須SUM(tothome.P)+ SUM(totaway.P)得到P.

+0

謝謝,我不知道如何合併therysys,我很新的SQL,所以嘗試但沒有得到遠 –