我有一个问题要解决:
-列出没有城市人口超过1百万的国家/地区的名称。
(我认为这将意味着COUNT(name)应该提取等于0的名称。我不确定如何用该语句表达短语。这是我的代码,其中有两个表。
<mysql>SELECT ct.code, ct.fullname, COUNT(name) FROM city AS cy JOIN country AS ct ON cy.country = ct.code
WHERE cy.population < 1000000
GROUP BY ct.code
ORDER BY ct.code ASC;
+------+---------------------------------------+-------------+
| code | fullname | COUNT(name) |
+------+---------------------------------------+-------------+
| ABW | Aruba | 1 |
| AFG | Afghanistan | 3 |
| AGO | Angola | 4 |
| AIA | Anguilla | 2 |
| ALB | Albania | 1 |
| AND | Andorra | 1 |
| ANT | Netherlands Antilles | 1 |
| ARE | United Arab Emirates | 5 |
答案 0 :(得分:1)
Count()
将只计算人口少于1000000的国家/地区中的所有城市。Where
子句基本上仅返回那些符合您给定条件的城市(人口少于1000000 )。不一定表示一个国家的所有城市都在1000000以下。Group By
中加上Having
子句,以便检查一个国家的所有城市,以使所有城市都没有population >= 1000000
尝试以下查询:
SELECT ct.code,
ct.fullname
FROM city AS cy
JOIN country AS ct ON cy.country = ct.code
GROUP BY ct.code, ct.fullname
HAVING SUM(cy.population > 1000000) = 0
ORDER BY ct.code ASC;
答案 1 :(得分:1)
SELECT cc.name
from city c, country cc
where CountryCode = code
group by cc.name
having MAX(c.population) < 1000000;