无法使用 group by 合并 sql 中的两个查询
not able to make union of two queries in sql using group by
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)
);
尝试删除别名,即 as s1
和 as s2
别名应该在子查询之外; MySQL 可能认为您正在尝试(毫无意义地)为分组标准设置别名。
删除 select 中的第二个别名和 group by 的别名,并为
中的子查询分配一个专有名称
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
您应该使用显式内部联接
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
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)
);
尝试删除别名,即 as s1
和 as s2
别名应该在子查询之外; MySQL 可能认为您正在尝试(毫无意义地)为分组标准设置别名。
删除 select 中的第二个别名和 group by 的别名,并为
中的子查询分配一个专有名称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
您应该使用显式内部联接
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