gpt4 book ai didi

SQL 仅选择拥有所有 3 个学位的成员

转载 作者:行者123 更新时间:2023-11-29 14:16:48 24 4
gpt4 key购买 nike

我有 3 个表。 Tourist 表、Junction 表和 Country 表。它们都由外键链接。 Junction 表有 tourist_idcountry_id Country 表有 name(国家名称)

Junction 通过 tourist_idcountry_id 链接 Tourist 和 Country

我想检索到过日本​​台湾加拿大的游客的所有信息

如果游客去过的国家多于或少于这 3 个国家,我不希望他们被退回。

如何编写此 SQL 查询?

最佳答案

您可以使用 group byhaving 来做到这一点:

select j.tourist_id
from junction j join
country c
on j.country_id = c.country_id
group by j.tourist_id
having count(distinct case when c.name in ('Japan', 'Canada', 'Taiwan') then c.name end) = 3 and
sum( (c.name not in ('Japan', 'Canada', 'Taiwan'))::int ) = 0;

关于SQL 仅选择拥有所有 3 个学位的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45291797/

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