如何進(jìn)行SQLSERVER中關(guān)于exists和in的簡單分析

這篇文章給大家介紹如何進(jìn)行SQL SERVER中關(guān)于exists 和 in的簡單分析,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

為河池等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及河池網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、河池網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

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。現(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ò),可以把它分享出去讓更多的人看到。

本文名稱:如何進(jìn)行SQLSERVER中關(guān)于exists和in的簡單分析
網(wǎng)站URL:http://muchs.cn/article28/gppgjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、、品牌網(wǎng)站建設(shè)企業(yè)建站、ChatGPT、域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司