【发布时间】:2015-10-26 14:01:59
【问题描述】:
我正在尝试为以下要求编写查询。给定两个表,City 和 Country,其描述如下。打印所有大洲的名称(键:Country.Continent)以及平均城市人口四舍五入到最接近的整数。
City
+-------------+----------+
| Field | Type |
+-------------+----------+
| ID | int(11) |
| Name | char(35) |
| CountryCode | char(3) |
| District | char(20) |
| Population | int(11) |
+-------------+----------+
国家
+----------------+-------------+
| Field | Type |
+----------------+-------------+
| Code | char(3) |
| Name | char(52) |
| Continent | char(50) |
| Region | char(26) |
| SurfaceArea | float(10,2) |
| IndepYear | smallint(6) |
| Population | int(11) |
| LifeExpectancy | float(3,1) |
| GNP | float(10,2) |
| GNPOld | float(10,2) |
| LocalName | char(45) |
| GovernmentForm | char(45) |
| HeadOfState | char(60) |
| Capital | int(11) |
| Code2 | char(2) |
+----------------+-------------+
PS #1:City.CountryCode 和 Country.Code 是同一个键。 PS #2:没有城市的大陆不应包含在输出中。
我尝试了以下查询,但其中似乎有问题, 请纠正我。
select Country.Continent,round(avg(City.population),0) from City
join Country
on City.CountryCode=Country.Code
where City.ID<>NULL
Group by Country.Continent;
【问题讨论】:
-
错误信息是什么?
-
试试
where City.ID is not null -
提示:城市人口的平均值不是大陆的人口。
-
没有错误消息,但它没有返回任何行,即使它应该返回一些行。
-
@GordonLinoff,每个大陆的平均城市人口是每个大陆的平均人口对吧?