mysql - 各大洲平均人口

标签 mysql sql

我正在尝试编写满足以下要求的查询。给定两个表,城市和国家/地区,其描述如下。打印所有大陆的名称(键: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;

完美运行,没有错误

enter image description here

关于mysql - 各大洲平均人口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33347675/

相关文章:

php - 每个特定的工作日,sql 和 php

mysql - 使用 DataGrip 的安全删除/更新模式

sql - generate_series() 在 DB2 中等效

sql - SQL 中的 SELECT DISTINCT 对 COLUMN

php - 如果存在则更新,否则插入(没有 ON DUPLICATE KEY UPDATE)

python - 错误 1045 (28000) : Access denied

SQL - 使用正则表达式更新列中的字符串

sql - 在事务中包装插入对 Sql Server 的性能有多大帮助?

sql - 如何返回嵌套的 JSON?

javascript - 如何在 HTML 中为价格添加自动点?