2017-09-25 107 views
1
select sum(total) from (
(select sum(team1_score) as total from country,match_results where country_name=team1 group by country_name as s1) 
UNION ALL 
(select sum(team2_score) as total from country,match_results where country_name=team2 group by country_name as s2) 
); 

回答

0

select sum(total) from (
    select sum(team1_score) as total 
    from country,match_results 
    where country_name=team1 
    group by country_name 
    UNION ALL 
    select sum(team2_score) 
    from country,match_results 
    where country_name=team2 
    group by country_name 
) T 

刪除在選擇第二別名和別名爲組和assigna真名的子查詢,您應該使用顯式的內部連接

select sum(total) from (
    select sum(team1_score) as total 
    from country 
    inner join match_results on country.country_name=team1 and 
    match_results.team1=team1 
    group by country_name 
UNION ALL 
    select sum(team2_score) 
    from country 
    inner join match_results on country.country_name=team2 and 
    match_results.team2=team2 
    group by country_name 
) T 
+0

非常感謝scaisEdge,兩種解決方案現在工作得很好。但在第二個建議使用內部連接,你可以解釋爲什麼使用兩個條件 country.country_name = team2和match_results.team2 = team2? –

+0

是相同的..我hav eopted這個,因爲更容易理解,但你也可以使用國家內部聯接match_results country.country_name = match_results.country_name和country.country_name = team1 ...是一樣的...使用傳遞屬性或不結果不改變 – scaisEdge

+0

@nikhilkekan好,如果我的回答是正確的請標記爲接受...看到這裏如何 http://meta.stackexchange.com/questions/5234/how-does -accepting-的回答工作 – scaisEdge

1

嘗試移除即別名as s1as s2

+0

試過沒有工作,我還是謝謝你 –

0

別名應是子查詢外,使用組中的SQL兩個查詢的結合; MySQL可能認爲你試圖(毫無意義地)混淆了分組標準。

+0

嘗試沒有工作,無論如何謝謝 –