except 语法
SQL2000不支持 SQL2005和以上版本才支持
附赠:
Except
查找两个集合之间不同的项,可以选择保留重复项。
语法
Except(«Set1», «Set2»[, ALL])
注释
在查找不同的项之前先消除两个集合中的重复项。可选的 ALL 标志保留重复项。清除 «Set1» 中的匹配重复项并保留非匹配重复项。
在SQL200中可以使用Union
例如:
1、
select * from aexcept
select * form b
应书写为:
select * from aunion
select * form b
2、
select * from aexcept all
select * form b
应书写为:
select * from aunion all
select * form b
我来普及一下知识这里的SQL,使用以下的测试表,与测试数据
CREATE TABLE union_tab_1 (
id INT,
val VARCHAR(10)
)
CREATE TABLE union_tab_2 (
id INT,
val VARCHAR(10)
)
INSERT INTO union_tab_1 VALUES(1, 'A')
INSERT INTO union_tab_1 VALUES(2, 'B')
INSERT INTO union_tab_1 VALUES(3, 'C')
INSERT INTO union_tab_2 VALUES(1, 'A')
INSERT INTO union_tab_2 VALUES(1, 'A')
INSERT INTO union_tab_2 VALUES(2, 'B')
INSERT INTO union_tab_2 VALUES(4, 'D')
MINUS / EXCEPT– 返回第一个表中有、第二个表中没有的数据
Oracle
SQL>SELECT * FROM union_tab_1
2 MINUS
3 SELECT * FROM union_tab_2
ID VAL
---------- --------------------
3 C
SQL>SELECT * FROM union_tab_2
2 MINUS
3 SELECT * FROM union_tab_1
ID VAL
---------- --------------------
4 D
SQL Server
1>SELECT * FROM union_tab_1
2>EXCEPT
3>SELECT * FROM union_tab_2
4>go
id val
----------- ----------
3 C
(1 行受影响)
1>SELECT * FROM union_tab_2
2>EXCEPT
3>SELECT * FROM union_tab_1
4>go
id val
----------- ----------
4 D
(1 行受影响)
通过 SET SHOWPLAN_TEXT ON 查看 查询计划.
我这里的测试表记录数量很小, 还没有索引, 因此没有参考价值.
1>SET SHOWPLAN_TEXT ON
2>go
1>SELECT * FROM union_tab_1
2>WHERE
3> id NOT IN
4> (SELECT id FROM union_tab_2)
5>go
StmtText
--------------------------------------------------------------------------------
-
SELECT * FROM union_tab_1
WHERE
id NOT IN
(SELECT id FROM union_tab_2)
(1 行受影响)
StmtText
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------
|--Nested Loops(Left Anti Semi Join, WHERE:([Test].[dbo].[union_tab_1].[id] IS
NULL OR [Test].[dbo].[union_tab_2].[id] IS NULL OR [Test].[dbo].[union_tab_1].[
id]=[Test].[dbo].[union_tab_2].[id]))
|--Table Scan(OBJECT:([Test].[dbo].[union_tab_1]))
|--Table Scan(OBJECT:([Test].[dbo].[union_tab_2]))
(3 行受影响)
1>SELECT * FROM union_tab_1
2>EXCEPT
3>SELECT * FROM union_tab_2
4>go
StmtText
----------------------------------------------------------------
SELECT * FROM union_tab_1
EXCEPT
SELECT * FROM union_tab_2
(1 行受影响)
StmtText
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------
|--Nested Loops(Left Anti Semi Join, OUTER REFERENCES:([Test].[dbo].[union_tab
_1].[id], [Test].[dbo].[union_tab_1].[val]))
|--Sort(DISTINCT ORDER BY:([Test].[dbo].[union_tab_1].[id] ASC, [Test].[d
bo].[union_tab_1].[val] ASC))
||--Table Scan(OBJECT:([Test].[dbo].[union_tab_1]))
|--Top(TOP EXPRESSION:((1)))
|--Table Scan(OBJECT:([Test].[dbo].[union_tab_2]), WHERE:([Test].[db
o].[union_tab_1].[id] = [Test].[dbo].[union_tab_2].[id] AND [Test].[dbo].[union_
tab_1].[val] = [Test].[dbo].[union_tab_2].[val]))
(5 行受影响)
1>SET SHOWPLAN_TEXT OFF
2>go
1>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)