MySQL:需要MySQL逻辑的帮助

时间:2015-10-24 21:20:00

标签: mysql

我正在努力解决MySQL问题而且我几乎拥有它...... 问题是......

您将获得什么样的查询来获得人口超过500,000的墨西哥所有城市?按人口按降序排列结果。

以下是我正在处理的表...

我当前的代码......

SELECT cities.name, cities.population
FROM countries
LEFT JOIN cities
ON countries.id = cities.country_id
WHERE cities.population < 500000

我觉得我应该非常接近。有什么指针吗?

2 个答案:

答案 0 :(得分:1)

想出来。我需要的只是更多细节!

旧代码......

SELECT cities.name, cities.population
FROM countries
LEFT JOIN cities
ON countries.id = cities.country_id
WHERE cities.population < 500000

新代码......

SELECT cities.name, cities.population
FROM countries 
LEFT JOIN cities
ON countries.id = cities.country_id
WHERE countries.name = 'Mexico' AND cities.population < 500000
GROUP BY cities.name
ORDER BY cities.name DESC

答案 1 :(得分:0)

问题出在哪里?看起来很简单......没有测试;)

选择t1.name,t1.population 来自国家t0加入城市t1     t0.id = t1.country_id 和t1.population&gt; 500000 由t1.population desc命令