两个有很多数据的完全一样结构的表,如何获得不包含在某一个表中的,但包含在另外一个表中的数据??

蝈蝈俊 2003-05-10 02:15:56
两个有很多数据的完全一样结构的表,如何获得不包含在某一个表中的,但包含在另外一个表中的数据??

有如下两个表(Table1,Table2),数据结构完全一样。不过Table1 包含 Table2的所有记录和一些其它数据,如何获得Table1中有,但是Table2中没有的数据??

(这两个表都有非常巨大的数据量)如果用 not in (1,2l,2,32) 这样的方式,sql 语句长度就会非常长,不适合再用了。

下面是一个只有少量数据的例子
create table Table1(id int);

insert table1 values(1);
insert table1 values(2);
insert table1 values(3);
insert table1 values(4);
insert table1 values(5);
insert table1 values(6);
insert table1 values(7);
insert table1 values(8);
insert table1 values(9);

create table Table2(id int);
insert Table2 values(1);
insert Table2 values(3);
insert Table2 values(8);

我如何得到
2
4
5
6
7
9
这些值
...全文
95 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-05-11
  • 打赏
  • 举报
回复
并运算
select c1,c2 from t1
union all
select c1,c2 from t2

差:
c1-c2:

select * from t1 where not exists(select 1 from t2 where t1.c1=t2.c1 and t1.c2=t2.c2)

c2-c1:

select * from t2 where not exists(select 1 from t1 where t1.c1=t2.c1 and t1.c2=t2.c2)

交:
select * from t1 where exists(select 1 from t2 where t1.c1=t2.c1 and t1.c2=t2.c2)


除:
create table #1(A char(1),B char(1),C char(1),D char(1))
insert #1 values('a','b','c','d')
insert #1 values('a','b','e','f')
insert #1 values('b','c','e','f')
insert #1 values('e','d','c','d')
insert #1 values('e','d','e','f')
insert #1 values('a','b','d','e')

create table #2 (A char(1),B char(1))
insert #2 values('c','d')
insert #2 values('e','f')

select a,b from #1 bb where exists(select 1 from (select distinct #1.a,#1.b,#2.a c,#2.b d from #1,#2) aa where aa.a=bb.a and aa.b=bb.b and aa.c=bb.c and aa.d=bb.d) group by a,b having count(*)>1




drop table #1
drop table #2
pengdali 2003-05-11
  • 打赏
  • 举报
回复
并运算
select c1,c2 from t1
union all
select c1,c2 from t2

差:
c1-c2:

select * from t1 where not exists(select 1 from t2 where t1.c1=t2.c1 and t1.c2=t2.c2)

c2-c1:

select * from t2 where not exists(select 1 from t1 where t1.c1=t2.c1 and t1.c2=t2.c2)

交:
select * from t1 where exists(select 1 from t2 where t1.c1=t2.c1 and t1.c2=t2.c2)


除:
create table #1(A char(1),B char(1),C char(1),D char(1))
insert #1 values('a','b','c','d')
insert #1 values('a','b','e','f')
insert #1 values('b','c','e','f')
insert #1 values('e','d','c','d')
insert #1 values('e','d','e','f')
insert #1 values('a','b','d','e')

create table #2 (A char(1),B char(1))
insert #2 values('c','d')
insert #2 values('e','f')

select a,b from #1 bb where exists(select 1 from (select distinct #1.a,#1.b,#2.a c,#2.b d from #1,#2) aa where aa.a=bb.a and aa.b=bb.b and aa.c=bb.c and aa.d=bb.d) group by a,b having count(*)>1




drop table #1
drop table #2
benxie 2003-05-10
  • 打赏
  • 举报
回复
难道呀、
lynx1111 2003-05-10
  • 打赏
  • 举报
回复
ghj1976 (蝈蝈俊.net) :
听说csdn的论坛是你做的?
.
.
.
.
第一次看见四星得提问!
凑热闹喽!

psxfghost 2003-05-10
  • 打赏
  • 举报
回复
我仔细看了你的意思,你可以试试写一个取
表中你要的字段的字段相加串,如:字段1+字段2+字段3+......
然后赋给一个字串变量@temp,这样下次你要用直接用
exec('....'+@temp+'....')就可以了,要增加也容易!
蝈蝈俊 2003-05-10
  • 打赏
  • 举报
回复
已经搞定了。
^&^
刚发,就看到本页就有一个这样的问题:

http://expert.csdn.net/Expert/topic/1764/1764622.xml?temp=.4040491

select * from Table1 where id not in (select id from Table2 )


或者:

select Table1.* from Table1 left outer join Table2 on Table2.id=Table1.id
where Table2.id is null
psxfghost 2003-05-10
  • 打赏
  • 举报
回复
更正:
select * from Table1 a left join Table2 b on a.id=b.id where b.id is null
psxfghost 2003-05-10
  • 打赏
  • 举报
回复
也可以是:
select * from Table1 a left join Table2 b where a.id=b.id and b.id is null
psxfghost 2003-05-10
  • 打赏
  • 举报
回复
select * from Table1 where id not in(select id from Table2)
new_life 2003-05-10
  • 打赏
  • 举报
回复
select * from table1 where id not in (select id from table2)

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧