gpt4 book ai didi

sql - 如果第一个表的所有记录都存在于第二个表中,则返回 true

转载 作者:行者123 更新时间:2023-12-05 00:06:47 25 4
gpt4 key购买 nike

我有两个表:

declare @t1 table (id int)
declare @t2 table (id int)


insert into @t1
select 1 union select 3 union select 7

insert into @t2
select 1 union select 3 union select 7 union select 9 union select 4

select count(*) from @t1 t inner join @t2 t1 on t.id = t1.id

我得到上述查询的结果为 3。如果 t1 中的所有记录都存在于 t2 中,我需要 true 或 false。

这是真实表结构的简化示例。真实的表可能有数百万条记录,所以请告诉我一些优化的方法

最佳答案

SELECT CASE 
WHEN EXISTS (SELECT id
FROM @t1
EXCEPT
SELECT id
FROM @t2) THEN 0
ELSE 1
END

关于sql - 如果第一个表的所有记录都存在于第二个表中,则返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9737015/

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