cust_id | cust_name | cust_address | cust_city | cust_state | cust_country | cust_contact | cust_email |
---|---|---|---|---|---|---|---|
1000000001 | Village Toys | 200 Maple Lane | Detroit | MI | USA | John Smith | sales@villagetoys.com |
1000000002 | Kids Place | 333 South Lake Drive | Columbus | OH | USA | Michelle Green | NULL |
1000000003 | Fun4All | 1 Sunny Place | Muncie | IN | USA | Jim Jones | jjones@fun4all.com |
1000000004 | Fun4All | 829 Riverside Drive | Phoenix | AZ | USA | Denise L. Stephens | dstephens@fun4all.com |
1000000005 | The Toy Store | 4545 53rd Street | Chicago | IL | USA | Kim Howard | NULL |
--過濾出列表中不存在于數(shù)據(jù)庫中的項(xiàng) --返回結(jié)果為['1000000006','1000000007'] SELECT [Id] AS [cust_id] FROM ( VALUES('1000000004'),('1000000005'),('1000000006'),('1000000007') ) dt ([Id]) EXCEPT SELECT [cust_id] FROM [Customers] --過濾出列表中存在于數(shù)據(jù)庫中的項(xiàng) --返回結(jié)果為['1000000004','1000000005'] SELECT [Id] AS [cust_id] FROM ( VALUES('1000000004'),('1000000005'),('1000000006'),('1000000007') ) dt ([Id]) INTERSECT SELECT [cust_id] FROM [Customers]
--對(duì)于SQLServer 2008以前的版本 SELECT [Id] AS [cust_id] FROM ( SELECT '1000000004' UNION ALL SELECT '1000000005' UNION ALL SELECT '1000000006' UNION ALL SELECT '1000000007' ) dt ([Id]) INTERSECT --EXCEPT SELECT [cust_id] FROM [Customers]
//使用C#動(dòng)態(tài)生成SQL語句 var list = new Liststring>(){"1000000004","1000000005","1000000006","1000000007"}; string sqlQuery = string.Format($@" SELECT [Id] AS [cust_id] FROM ( VALUES('{string.Join("'),('", list)}') ) dt ([Id] INTERSECT --EXCEPT SELECT [cust_id] FROM [Customers]" );
更多參考
Set Operators - EXCEPT and INTERSECT
Set Operators - UNION
到此這篇關(guān)于SQL Server中的集合運(yùn)算: UNION, EXCEPT和INTERSECT的文章就介紹到這了,更多相關(guān)SQL Server中的集合運(yùn)算內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:汕頭 濟(jì)源 廣東 武威 濟(jì)寧 臺(tái)州 安徽 泰安
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQL Server中的集合運(yùn)算: UNION, EXCEPT和INTERSECT示例代碼詳解》,本文關(guān)鍵詞 SQL,Server,中的,集合,運(yùn)算,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。