2017-09-25 67 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

尝试没有工作,无论如何谢谢 –