Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

mysql - 编写查询以显示首都及其所在国家中所有城市中最大的城市的名称(Write a query that displays the names of cities and their countries when the capital city is the largest of all cities listed for that country)

I have a database called world .

(我有一个名为world的数据库。)

This database has two tables: city and country .

(该数据库有两个表: citycountry 。)

city 's columns are: name , ID , Population , countryCode .

(citycountryCodenameIDPopulationcountryCode 。)

country 's columns are: Code , name , countryPopulation , Capital .

(country的列是: CodenamecountryPopulationCapital 。)

  • countryCode in city table = Code in country table

    (city表中的countryCode = country表中的Code)

  • ID in city table = capital in country table

    (city表中的ID = country表中的capital)

Write a query that displays the names of cities and their countries when the capital city is the largest of all cities listed for that country.

(编写一个查询,以显示首都及其所在国家中所有城市中最大的城市的名称。)

Here is the code that displays the largest cities list:

(这是显示最大城市列表的代码:)

SELECT MAX(c.Population) AS Population, c.Name AS City, cou.Name AS Country
FROM city c, country cou
WHERE c.CountryCode = cou.Code
GROUP BY cou.Name

Suppose I have that information from previous code:

(假设我有以前代码中的信息:)

Population    City             Country
1000000       Washington DC    USA
993943210     Sao Paulo        Brazil
1911919       Dubai            UAE

My query should show all cities in the USA because Washington DC is the capital of USA and all cities in the UAE because Dubai is the capital of UAE but should not show cities in Brazil because Sao Paulo is not the capital of Brazil.

(我的查询应显示美国的所有城市,因为华盛顿特区是美国的首都,而阿联酋的所有城市,因为迪拜是阿联酋的首都,但不应该显示巴西的城市,因为圣保罗不是巴西的首都。)

  ask by Tiger07 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need your customer's names as well as the names of the cities in which they live.

(您需要客户的名字以及他们居住的城市的名字。)

The names of the cities are stored in a separate table called "cities".

(城市名称存储在称为“城市”的单独表中。)

RIGHT
SELECT customers.name, cities.name
FROM customers
OUTER JOIN cities
ON cities.id=customers.city_id;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...