34,838
社区成员




declare @小组表 table (小组编码 int,小组名称 varchar(1))
insert into @小组表
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e' union all
select null,null union all
select 7,'g' union all
select 8,'h' union all
select 9,'i' union all
select 10,'j'
declare @登记表 table (小组 int)
insert into @登记表
select null union all
select 3 union all
select 4 union all
select 5 union all
select 6
SELECT 小组编码,小组名称
FROM @小组表 a where
not exists (select * from @登记表 b where b.小组=a.小组编码)
/*
小组编码 小组名称
----------- ----
2 b
7 g
8 h
9 i
10 j
*/
SELECT 小组编码,小组名称
FROM @小组表 where 小组编码 not in (select 小组 from @登记表)
/*
小组编码 小组名称
----------- ----
(0 row(s) affected)
*/
declare @小组表 table (小组编码 int,小组名称 varchar(1))
insert into @小组表
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e' union all
select 6,'f' union all
select 7,'g' union all
select 8,'h' union all
select 9,'i' union all
select 10,'j'
declare @登记表 table (小组 int)
insert into @登记表
select 1 union all
select 3 union all
select 4 union all
select 5 union all
select 6
SELECT 小组编码,小组名称
FROM @小组表 a where
not exists (select * from @登记表 b where b.小组=a.小组编码)
/*
小组编码 小组名称
----------- ----
2 b
7 g
8 h
9 i
10 j
*/
SELECT 小组编码,小组名称
FROM @小组表 where 小组编码 not in (select 小组 from @登记表)
/*
小组编码 小组名称
----------- ----
2 b
7 g
8 h
9 i
10 j
*/
在sqlserver2005中,似乎in 与exists的效率相当,是有优化