gpt4 book ai didi

sql - 在 In 子句中传递时获取不在表中的记录?

转载 作者:搜寻专家 更新时间:2023-10-30 22:35:57 25 4
gpt4 key购买 nike

我确定问题的标题需要更改,但我不确定该放什么。

我在两个表中传递 In 子句想要获取不在两个表中的记录。

表A包含ID 1,2B表包含ID 3,4

我正在传递我的 In 子句 (1,2,3,4,5,6)

我在找东西

1 TableA
2 TableA
3 TableB
4 TableB
5 Not Found
6 Not Found

我正在使用 union all 从表 A 和表 B 中获取项目,不确定如何在两个表中获取未找到的记录?

最佳答案

这是一种方法。它使用 exists 检查表是否包含 id:

select id,
(case when inA = 1 and inB = 1 then 'Both'
when inA = 1 then 'TableA'
when inB = 1 then 'TableB'
else 'Not Found'
end) as status
from (select id,
(case when exists (select 1 from tableA a where a.id = ids.id then 1 else 0 end) as inA,
(case when exists (select 1 from tableB b where b.id = ids.id then 1 else 0 end) as inB
from (values (1), (2), (3), (4), (5), (6)) as ids(id)
) i;

当然,如果您不想要'Both' 行,您可以添加where inA = 0 or inB = 0

关于sql - 在 In 子句中传递时获取不在表中的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37577247/

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