gpt4 book ai didi

mysql - "Invert"输出列出此时未打印/列出的所有内容

转载 作者:行者123 更新时间:2023-11-29 19:21:29 26 4
gpt4 key购买 nike

使用 SQL。我正在尝试列出所有城市的人口总数少于 10 万的每个国家/地区的名称。

下面的代码为我提供了拥有超过 100 000 人的城市的每个国家/地区,因此根据术语,我试图“反转”输出以列出此时未打印/列出的所有内容。

建议?

Select distinct country.Name from country,city
where city.CountryCode = country.Code and city.population > 100000;

最佳答案

处理此问题的典型方法是使用聚合和having:

select co.Name
from country co left join
city ci
on ci.CountryCode = co.Code
group by co.Name
having coalesce(max(ci.population), 0) <= 100000;

coalesce()left join 考虑到没有城市的国家/地区。

作为引用,您问题中版本的等效查询:

select co.Name
from country co left join
city ci
on ci.CountryCode = co.Code
group by co.Name
having max(ci.population) >= 100000;

关于mysql - "Invert"输出列出此时未打印/列出的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42416041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com