【问题标题】:Average Population of Each Continent各大洲的平均人口
【发布时间】: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,每个大陆的平均城市人口是每个大陆的平均人口对吧?

标签: mysql sql


【解决方案1】:
select country.continent,floor(avg(city.population))
 from city,country
where city.countrycode=country.code
group by country.continent;

完美无误

【讨论】:

  • 我实际上发现你的答案的逻辑最容易理解。特别是如果您按以下方式订购。谢谢! SELECT COUNTRY.CONTINENT, FLOOR(AVG(CITY.POPULATION)) FROM COUNTRY, CITY WHERE COUNTRY.CODE = CITY.COUNTRYCODE GROUP BY COUNTRY.CONTINENT;
【解决方案2】:
select country.continent,floor(avg(city.population))
from country inner join city
on country.Code=city.countrycode 
group by country.continent;

【讨论】:

    【解决方案3】:
    select country.continent,floor(avg(city.population)) from country,city 
    where country.code = city.countrycode
    group by country.continent;
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    【解决方案4】:

    我对这个问题使用了以下查询:

    Select COUNTRY.Continent, FLOOR(AVG(CITY.POPULATION)) 
    From CITY join COUNTRY on CITY.CountryCode = COUNTRY.Code  
    Group by country.continent
    

    【讨论】:

      【解决方案5】:

      它是由City.ID&lt;&gt;NULL 引起的。结果将始终为null,并且不会出现任何行。

      改为使用where City.ID is not null

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

      例子:

      select null &lt;&gt; nullnull

      同时

      select null is not nullfalse

      如您所见,null 无法使用 =、 运算符进行比较,即使与它自己也是如此。

      同样

      select 1 &lt;&gt; null 给出null,但是

      select 1 is not null 导致true

      【讨论】:

        【解决方案6】:
        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;
        

        【讨论】:

          【解决方案7】:

          你可以在 oracle 中使用它-

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

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

              【解决方案9】:

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

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

              【讨论】:

                【解决方案10】:

                这是我想出的解决方案:

                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;

                【讨论】:

                  【解决方案11】:
                  select country.continent,
                     floor(avg(city.population)) from country,city where country.code=city.countrycode group by country.continent;
                  

                  【讨论】:

                  • 请在您的答案中添加一些解释,以便其他人可以从中学习
                  【解决方案12】:
                  SELECT COUNTRY.CONTINENT, FLOOR(AVG(CITY.POPULATION))
                  FROM CITY INNER JOIN COUNTRY ON CITY.COUNTRYCODE=COUNTRY.CODE
                  GROUP BY COUNTRY.CONTINENT;
                  

                  【讨论】:

                  • 请在您的答案中添加一些解释,以便其他人可以从中学习
                  【解决方案13】:
                  SELECT p.Continent , FLOOR(AVG(c.Population))
                  FROM Country p, City c 
                  WHERE p.Code = C.CountryCode 
                  GROUP BY p.Continent ;
                  

                  【讨论】:

                    【解决方案14】:
                    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 
                    

                    【讨论】:

                      【解决方案15】:
                      select country.continent, FLOOR (AVG (city.population))
                      from country
                      join city on country.code=city.countrycode
                      group by country.continent;
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2013-03-25
                        • 1970-01-01
                        • 2022-11-10
                        • 1970-01-01
                        • 2016-03-11
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多