gpt4 book ai didi

SQL : select unmatching records from 2 tables

转载 作者:行者123 更新时间:2023-12-01 00:09:16 26 4
gpt4 key购买 nike

我有 2 个表,我想从第一个表中检索 id_apartment 没有出现在第二个表中的行:

id | id_floor | id_apartment 
----+----------+--------------
1 | 0 | 101
2 | 1 | 101
3 | 1 | 102
4 | 1 | 103
5 | 1 | 104
6 | 2 | 201
7 | 2 | 202
8 | 2 | 203


table2.id | table2.guest | table2.apartment_id
----+---------------+--------------
1 | 65652 | 101
2 | 65653 | 101
3 | 65654 | 101
4 | 65655 | 101
5 | 65659 | 102
6 | 65656 | 201
7 | 65660 | 202
8 | 65661 | 202
9 | 65662 | 202
10 | 65663 | 203

预期输出:
floor | number
-------+--------
1 | 103
1 | 104

我尝试使用 LEFT、INNER 和 RIGHT 连接,但我总是得到 EMPTY 结果。我该如何管理?

最佳答案

您可以使用 not exists :

select *
from table1 t1
where not exists(
select 1
from table2 t2
where t1.id_apartment = t2.apartment_id
)

关于SQL : select unmatching records from 2 tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60400001/

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