這篇文章給大家介紹如何進(jìn)行SQL SERVER中關(guān)于exists 和 in的簡單分析,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
In與Exists這兩個(gè)函數(shù)是差不多的,但由于優(yōu)化方案不同,通常NOT Exists要比NOT IN要快,因?yàn)镹OT EXISTS可以使用結(jié)合算法二NOT IN就不行了,而EXISTS則不如IN快,因?yàn)檫@時(shí)候IN可能更多的使用結(jié)合算法。
如圖,現(xiàn)在有兩個(gè)數(shù)據(jù)集,左邊表示#tempTable1,右邊表示#tempTable2?,F(xiàn)在有以下問題:
1.求兩個(gè)集的交集?
2.求tempTable1中不屬于集#tempTable2的集?
先創(chuàng)建兩張臨時(shí)表:
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);
比如,我現(xiàn)在以#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)
關(guān)于如何進(jìn)行SQL SERVER中關(guān)于exists 和 in的簡單分析就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章標(biāo)題:如何進(jìn)行SQLSERVER中關(guān)于exists和in的簡單分析-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://jinyejixie.com/article32/dsessc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、電子商務(wù)、ChatGPT、全網(wǎng)營銷推廣、外貿(mào)建站、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容