RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
如何进行SQLSERVER中关于exists和in的简单分析

这篇文章给大家介绍如何进行SQL SERVER中关于exists 和 in的简单分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

为河池等地区用户提供了全套网页设计制作服务,及河池网站建设行业解决方案。主营业务为成都网站制作、网站建设、外贸网站建设、河池网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

In与Exists这两个函数是差不多的,但由于优化方案不同,通常NOT Exists要比NOT IN要快,因为NOT EXISTS可以使用结合算法二NOT IN就不行了,而EXISTS则不如IN快,因为这时候IN可能更多的使用结合算法。

如图,现在有两个数据集,左边表示#tempTable1,右边表示#tempTable2。现在有以下问题:

1.求两个集的交集?

2.求tempTable1中不属于集#tempTable2的集?

先创建两张临时表:

create table #tempTable1(  argument1 nvarchar(50),  argument2 varchar(20),  argument3 datetime,  argument4 int);insert into #tempTable1(argument1,argument2,argument3,argument4)values('preacher001','13023218757',GETDATE()-1,1);insert into #tempTable1(argument1,argument2,argument3,argument4)values('preacher002','23218757',GETDATE()-2,2);insert into #tempTable1(argument1,argument2,argument3,argument4)values('preacher003','13018757',GETDATE()-3,3);insert into #tempTable1(argument1,argument2,argument3,argument4)values('preacher004','13023257',GETDATE()-4,4);insert into #tempTable1(argument1,argument2,argument3,argument4)values('preacher005','13023218',GETDATE()-5,5);insert into #tempTable1(argument1,argument2,argument3,argument4)values('preacher006','13023218',GETDATE()-6,6);insert into #tempTable1(argument1,argument2,argument3,argument4)values('preacher007','13023218',GETDATE()-7,7);insert into #tempTable1(argument1,argument2,argument3,argument4)values('preacher008','13023218',GETDATE()-8,8);create table #tempTable2(  argument1 nvarchar(50),  argument2 varchar(20),  argument3 datetime,  argument4 int);insert into #tempTable2(argument1,argument2,argument3,argument4)values('preacher001','13023218757',GETDATE()-1,1);insert into #tempTable2(argument1,argument2,argument3,argument4)values('preacher0010','23218757',GETDATE()-10,10);insert into #tempTable2(argument1,argument2,argument3,argument4)values('preacher003','13018757',GETDATE()-3,3);insert into #tempTable2(argument1,argument2,argument3,argument4)values('preacher004','13023257',GETDATE()-4,4);insert into #tempTable2(argument1,argument2,argument3,argument4)values('preacher009','13023218',GETDATE()-9,9);

比如,我现在以#tempTable1和#tempTable2的argument1作为参照

1.求两集的交集:

1)in 方式

select * from #tempTable2 where argument1 in(select argument1 from #tempTable1)

2)exists 方式

select * from #tempTable2 t2 where exists (select * from #tempTable1 t1 where t1.argument1=t2.argument1)

2.求tempTable1中不属于集#tempTable2的集

1)in 方式

select * from #tempTable1 where argument1 not in(select argument1 from #tempTable2)

2)exists 方式

select * from #tempTable1 t1 where not exists (select * from #tempTable2 t2 where t1.argument1=t2.argument1)

关于如何进行SQL SERVER中关于exists 和 in的简单分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


本文标题:如何进行SQLSERVER中关于exists和in的简单分析
分享网址:http://scyingshan.cn/article/gppgjp.html