我正在尝试编写满足以下要求的查询。给定两个表,城市和国家/地区,其描述如下。打印所有大陆的名称(键: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;
最佳答案
select country.continent,floor(avg(city.population))
from city,country
where city.countrycode=country.code
group by country.continent;
完美运行,没有错误
关于mysql - 各大洲平均人口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33347675/