gpt4 book ai didi

SQL:EXCEPT 查询

转载 作者:行者123 更新时间:2023-12-04 04:33:41 24 4
gpt4 key购买 nike

这是我试图实现的基本示例:

create table #testing (
tab varchar(max), a int, b int, c int )

insert into #testing VALUES ('x',1, 2, 3)
insert into #testing VALUES ('y',1, 2, 3)
insert into #testing VALUES ('x', 4, 5, 6)

select * from #testing

这将产生表:
 tab     a    b    c
-----------------------
x 1 2 3
y 1 2 3
x 4 5 6

然后我想根据 a、b、c 的值比较“tab”上的行:
select a,b,c from #testing where tab = 'x'
except
select a,b,c from #testing where tab= 'y'

这给了我我期待的答案:
a    b    c
------------
4 5 6

但是我还想在我的结果集中包含 Tab 列,所以我想要这样的东西:
 Select tab,a,b,c from #testing where ????
(select a,b,c from #testing where tab = 'x'
except
select a,b,c from #testing where tab= 'y')

我将如何实现这一目标?

最佳答案

使用 not exists :

select a.*
from #testing a
where a.tab = 'x'
and not exists (
select *
from #testing t
where t.a = a.a and t.b = a.b and t.c = a.c and t.tab = 'y'
)

在这里你可以获得 SQL Fiddle 演示:DEMO

关于SQL:EXCEPT 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20119489/

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