0

我正在尝试学习数据库(特别是 MySQL),并且正在复习我购买的教科书中的问题(没有提供解决方案手册)。我已经实现了一个问题的 A 部分,但我很困惑如何做 B 部分。 B 部分要求获得与 A 部分相同的结果,除了使用 UNION。我想知道是否有人可以解释?

提前致谢

我认为A部分的答案:

SELECT Country, COUNT(City) from country LEFT OUTER JOIN city 
ON city.CountryId = country.CountryId group by Country;

B 部分:将 A 部分的查询写成 UNION?

4

2 回答 2

0

试试这个

select country from country group by country
 union
select count(city) from city group by city

使用“联合”时,您不会在单独的列中获得结果。您将在同一列中获得结果

于 2012-11-21T10:40:03.297 回答
0
select c1.countrynm,count(c2.citynm) 
from country c1 
join city c2
on c2.countryid=c1.countryid
group by c1.countrynm
union select countrynm,0 from country where 7=3;

mysql> select c1.countrynm,count(c2.citynm)
-> from country c1
-> join city c2
-> on c2.countryid=c1.countryid
-> group by c1.countrynm
-> union select countrynm,0 from country where 7=3;
+-----------+------------------+
| countrynm | count(c2.citynm) |
+-----------+------------------+
| France    |                2 |
| Germany   |                1 |
+-----------+------------------+
于 2012-11-21T10:56:33.137 回答