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

蝈蝈俊 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
这些值
...全文
121 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)
内容概要:本文围绕基于多元宇宙优化算法(Multi-Verse Optimizer, MVO)的主动配电网优化调度展开研究,重点探讨了在“源-荷-储”协同互动背景下,如何实现配电网系统的高效、可靠与经济运行。研究以IEEE33节点标准配电系统为仿真平台,综合考虑分布式电源(如风电、光伏)、负荷需求及储能系统的不确定性与随机特性,构建了多主体协同优化调度模型。通过引入多元宇宙优化算法对目标函数进行求解,有效提升了优化效率与收敛精度,实现了系统运行成本最小化、网损降低与电压稳定性增强等多重目标。该研究不仅验证了MVO算法在复杂电力系统优化问题中的优越性能,也为含高比例可再生能源接入的主动配电网提供了科学的调度策略支持。; 适合人群:电力系统、能源互联网、智能电网及相关领域的科研人员、高校研究生以及从事配电网优化、分布式能源管理的技术工程师。; 使用场景及目标:①应用于主动配电网的日前或实时优化调度场景,提升系统对可再生能源的消纳能力;②为“源-荷-储”协调控制策略的设计提供算法支撑与仿真验证手段;③作为智能优化算法在电力系统中应用的教学与研究案例,推动MVO等新兴元启发式算法的实际落地。; 阅读建议:建议读者结合提供的Matlab代码深入理解模型构建与算法实现细节,重点关注目标函数设计、约束条件处理及MVO算法参数设置部分,并可通过修改源荷场景进行敏感性分析与对比实验,进一步掌握其在不同工况下的适应性与鲁棒性。

34,874

社区成员

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

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