每个大陆的平均人口

时间:2015-10-26 14:02:00

标签: 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;

14 个答案:

答案 0 :(得分:1)

select country.continent,floor(avg(city.population))
from country inner join city
on country.Code=city.countrycode 
group by country.continent;

它有效..

答案 1 :(得分:0)

City.ID<>NULL引起。结果将始终为null,并且不会显示任何行。

相反,请使用where City.ID is not null

Cf。:https://dev.mysql.com/doc/refman/5.0/en/working-with-null.html

示例:

select null <> null提供null

,而

select null is not null提供false

如您所见,null无法使用=,&lt;,&gt;进行比较运营商,甚至是自己。

相同

select 1 <> null提供null,但

select 1 is not null会产生true

答案 2 :(得分:0)

select country.continent,floor(avg(city.population))
 from city,country
where city.id is NOT NULL
  and city.countrycode=country.code
group by country.continent;

答案 3 :(得分:0)

SELECT COUNTRY.CONTINENT, FLOOR(AVG(CITY.POPULATION))
FROM CITY INNER JOIN COUNTRY ON CITY.COUNTRYCODE=COUNTRY.CODE
GROUP BY COUNTRY.CONTINENT;

这会对你有帮助。

答案 4 :(得分:0)

试试这个,

 select country.continent,
    floor(avg(city.population)) from country,city where country.code=city.countrycode group by country.continent;

答案 5 :(得分:0)

这将有效:

SELECT p.Continent , FLOOR(AVG(c.Population))
FROM Country p, City c 
WHERE p.Code = C.CountryCode 
GROUP BY p.Continent ;

答案 6 :(得分:0)

select country.continent,floor(avg(city.population)) from country,city 
where country.code = city.countrycode
group by country.continent;

答案 7 :(得分:0)

select country.continent,floor(avg(city.population))
 from city,country
where city.countrycode=country.code
group by country.continent;

完美无误地工作

enter image description here

答案 8 :(得分:0)

最好的办法是尝试,在尝试解决方案之前先尝试再尝试。 这是我想出的解决方案。

select co.continent,
floor(sum(ci.population) / count(ci.name))
from 
country co join city ci on ci.countrycode = co.code 
group by co.continent;

如果自己尝试,就会学到

总和

计数

分组依据

地板

最重要的是满意度。 编码愉快。

答案 9 :(得分:0)

我确实做到了–努力

    select country.continent ,truncate(avg(city.population),0) as dd
    from city
    left join country on city.countrycode=country.code
    group by country.continent having country.continent is not NULL
    order by dd 

答案 10 :(得分:0)

您可以在oracle中使用它-

select cu.continent
     , floor(avg(c.population)) 
  from city c 
  join country cu 
    on c.countrycode = cu.code 
 group by cu.continent;

The output you will be getting is -

答案 11 :(得分:0)

您可以使用Oracle运行以下代码。

SELECT CO.Continent, FLOOR(AVG((C.Population)))
FROM CITY C 
     INNER JOIN COUNTRY CO ON C.CountryCode = CO.Code
     GROUP BY CO.Continent;

答案 12 :(得分:0)

我在 mysql 中使用了以下内容:

SELECT COUNTRY.CONTINENT, FLOOR(AVG(CITY.POPULATION))
FROM CITY
JOIN COUNTRY
ON CITY.COUNTRYCODE=COUNTRY.CODE
GROUP BY COUNTRY.CONTINENT;

答案 13 :(得分:-1)

试试这段代码:

 select country.continent, FLOOR (AVG (city.population))
 from country
 join city on country.code=city.countrycode
 group by country.continent;