简体   繁体   English

SQL:哪个国家的城市最多?

[英]SQL: What country has the most cities?

Hello I started using MySQL and I seem to be having trouble trying to nest formulas. 您好,我开始使用MySQL,尝试嵌套公式似乎有些麻烦。 I'm working on a problem and the question is, What country has the most cities? 我正在研究一个问题,问题是,哪个国家的城市最多?

I have two tables: 我有两个表:

CITY:
city
city_id
country_id

COUNTRY:
country 
country_id

I am able to join the two tables together to get the cities to match with the countries but after that I don't know how to count to the country that has the most cities. 我能够将两个表合并在一起,以使城市与国家/地区匹配,但是在那之后,我不知道如何算出拥有最多城市的国家/地区。

My current code is: 我当前的代码是:

SELECT city.city, country.country 
FROM city, country
WHERE city.country_id = country.country_id

From there I don't know how to add a count function without it coming back as as error. 从那里,我不知道如何添加计数函数而又不会作为错误返回。 I dont fully understand the basics of nesting. 我不完全了解嵌套的基础。

Thank you, any help is appreciated. 谢谢您的帮助。

You do not need to do nesting necessarily. 您不必一定要嵌套。 To simply know, which country has most number of cities, just use group by: 要简单地知道哪个国家的城市最多,只需使用group by:

select country_id, count(1)
from city
group by country_id

This will give you the number of cities in each country. 这将为您提供每个国家的城市数。 Then you could use a CTE to get the country with the largest number of cities. 然后,您可以使用CTE获取拥有最多城市的国家。

You need to GROUP BY if you want to use aggregate functions. 如果要使用聚合函数,则需要GROUP BY

Given the fact that you're very new to this I think you'll get a lot more out of this if you spend a few minutes reading up on some documentation. 考虑到您对此非常陌生,如果您花几分钟阅读一些文档,将会从中获得更多收益。 Don't worry, this is easy stuff so you'll understand this in no time. 别担心,这是一件容易的事,因此您很快就会了解。 Please have a look at the following basic info ( MySQL GROUP BY basic info ) regarding the use of GROUP BY in MySQL. 请查看以下有关MySQL中使用GROUP BY的基本信息( MySQL GROUP BY基本信息 )。 Your questions are answered in the topic regarding 'MySQL GROUP BY with aggregate functions'. 有关“带有聚合功能的MySQL GROUP BY”的主题,将回答您的问题。

Basic group by: 基本分组依据:

SELECT 
    status, COUNT(*)
FROM
    orders
GROUP BY status;

Group by using a join: 通过联接分组:

SELECT 
    status, SUM(quantityOrdered * priceEach) AS amount
FROM
    orders
        INNER JOIN
    orderdetails USING (orderNumber)
GROUP BY status;
SELECT x.country 
  FROM country x
  JOIN city y
    ON y.country_id = x.country_id
 GROUP
    BY x.country
 ORDER 
    BY COUNT(*) DESC LIMIT 1;

On the (fantastically unlikely) chance that the most civilised countries have equal numbers of cities, you would have to amend this a little. 如果文明程度最高的国家拥有相等数目的城市(极不可能),您将不得不对此稍作修改。

What makes this difficult is possible ties, ie two or more countries sharing the maximum number of cities. 造成这种困难的原因是可能的联系,即两个或更多国家共享最多的城市。 As of MySQL 8 you can use window functions to help you with this. 从MySQL 8开始,您可以使用窗口函数来帮助您。 Here I compare the country counts and their maximum and then pick the rows were the two match. 在这里,我比较了国家/地区和国家/地区的最大值,然后选择了两个匹配的行。

select *
from country
where (country_id, true) in -- true means it is a maximum city country
(
  select country_id, count(*) = max(count(*)) over()
  from city
  group by country_id
);

Try the following code: 尝试以下代码:

**select
    country.country_id,
    count(city.city_id)
from
    country
inner join
    city
on
    city.country_id=country.country_id
group by
    city.country_id
having
    count(city.city_id) =
    (SELECT
        max(count(city.city_id))
    FROM
        city
    GROUP BY
        city.city_id);**

You need to group by country_id to make sure all cities that are connected to one country_id can be counted. 您需要按country_id分组,以确保可以计算连接到一个country_id所有城市。

Where is a good approach, however it does not work together with "group by" as it will be accounted before the "group by" command. 哪里有个好的方法,但是它不能与“ group by”一起使用,因为它将在“ group by”命令之前进行说明。

The way you joined your data from different tables is not a very proper yet working way. 您从不同表中联接数据的方法不是一种非常合适但可行的方法。 I suggest to use the inner join in this case to make the command more obvious/better readable. 我建议在这种情况下使用内部联接使命令更明显/更易读。

Count() is used to count the number of cities that accumulate on one country max() is used to get the country with the most cities (highest count()). Count()用于计算在一个国家/地区上累积的城市数量。max()用于获得城市数量最多(最高count())的国家/地区。

Sorry it was my first post, my code looks terrible. 抱歉,这是我的第一篇文章,我的代码看上去很糟糕。

following my code again. 再次遵循我的代码。

select
    country.country_id,
    count(city.city_id)
from
    country
inner join
    city
on
    city.country_id=country.country_id
group by
    city.country_id
having
    count(city.city_id) =
    (SELECT
        max(count(city.city_id))
    FROM
        city
    GROUP BY
        city.city_id);

Best regards, 最好的祝福,

Jens 詹斯

SELECT country_id, count(1)
 FROM city
 GROUP BY country_id
 ORDER BY count(1) desc;

Try following Code - 尝试遵循以下代码-

SELECT *, 
  (SELECT COUNT(*) FROM cities WHERE cities.country_id=C.country_id) as cities_count 
FROM country C 
ORDER BY cities_count DESC
LIMIT 0,1

Also is joining the two tables necessary? 还需要将两个表连接起来吗? In your query, you said you need to find What country has the most cities? 在您的查询中,您说过需要找到哪个国家的城市最多?

Above query will only return one country with max cities. 上面的查询将仅返回一个城市最多的国家。

You can find the country like this: 您可以找到这样的国家:

SELECT MAX(c.id) FROM (SELECT COUNT(id) AS id FROM city group by country_id) c SELECT MAX(c.id)FROM(SELECT COUNT(id)AS id from country group by country_id)c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM