INTERSECT 交集集合运算,把集合共同拥有的记录返回来。
为了演示,首选创建两个变量表,并插入数据:
第一个表:
代码
DECLARE @T1 AS TABLE(ColName1 NVARCHAR(50),ColName2 NVARCHAR(50))
INSERT INTO @T1 VALUES ('tc1','tc2');
INSERT INTO @T1 VALUES ('tc3','tc4');
INSERT INTO @T1 VALUES ('tc5','tc6');
INSERT INTO @T1 VALUES ('tc13','tc14');
INSERT INTO @T1 VALUES ('tc1','tc2');
INSERT INTO @T1 VALUES ('tc3','tc4');
INSERT INTO @T1 VALUES ('tc5','tc6');
INSERT INTO @T1 VALUES ('tc13','tc14');
第二表:
代码
DECLARE @T2 AS TABLE(ColName1 NVARCHAR(50),ColName2 NVARCHAR(50))
INSERT INTO @T2 VALUES ('tc1','tc2');
INSERT INTO @T2 VALUES ('tc3','tc4');
INSERT INTO @T2 VALUES ('tc9','tc10');
INSERT INTO @T2 VALUES ('tc7','tc8');
INSERT INTO @T2 VALUES ('tc1','tc2');
INSERT INTO @T2 VALUES ('tc3','tc4');
INSERT INTO @T2 VALUES ('tc9','tc10');
INSERT INTO @T2 VALUES ('tc7','tc8');
INTERSECT交集运算:
SELECT * FROM @T1
INTERSECT
SELECT * FROM @T2
INTERSECT
SELECT * FROM @T2
结果,