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

蝈蝈俊 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
这些值
...全文
105 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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)
资源下载链接为: https://pan.quark.cn/s/abbae039bf2a 在计算机科学领域,编译原理是研究如何将编程语言转化为机器可执行代码的理论基础。其中,三地址代码(Three-Address Code,TAC)作为一种中间示形式,在编译器设计中经常被使用,尤其是在生成目标代码的阶段。本文将深入探讨三地址代码的概念、生成器的工作原理及其在编译过程中的作用。 三地址代码是一种简单的低级抽象语法树(AST)示,每条指令涉及三个操作数,通常包括两个源操作数和一个目的操作数。这种格式简化了代码优化和目标代码生成的复杂性。例如,一个简单的算术达式“x = y + z”在三地址代码中可能示为: 在这个例子中,“t1”是一个临时变量,存储了“y + z”的结果,然后这个结果被赋值给“x”。 生成三地址代码的过程通常发生在编译器的中间阶段,即语法分析之后,语义分析之前。这个阶段称为“代码生成”或“中间代码生成”。编译器通过词法分析器处理源代码,将其转化为标记流;接着,语法分析器根据上下文无关文法将标记流解析成抽象语法树。三地址代码生成器就是在这个阶段介入,它遍历AST,为每个节点生成对应的三地址指令。 在Turbo C3.0这样的编译器环境下,开发者可以实现自己的三地址代码生成器。虽然Turbo C3.0是一款较老的编译器,但其C语言编译器设计原理依然适用于现代编译器开发。开发过程中,我们需要考虑如下关键点: 符号管理:符号记录了程序中所有标识符的类型、作用域和关联地址,对于生成三地址代码至关重要,因为它提供了关于操作数的类型信息。 数据类型转换:编译器必须处理不同数据类型的运算,确保它们在三地址代码中正确示。例如,整型与浮点型之间的转换需要特别处理。

34,838

社区成员

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

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