日期:2014-05-17  浏览次数:20472 次

请教啊,查询A表中未曾出现在B表和C表中的记录,sql2000
请用sql2000,另外数据记录有5万条以上,请尽量效率高,谢谢
A表
SQL code

id   
001 
002
003
004
005
006 


B表
SQL code

002
003
005


C表
SQL code

003
004
005


查询结果
001
006


------解决方案--------------------
SQL code
select * from A
where not exists
(
  select * from B where A.id = id
)
and
not exists
(
  select * from C where A.id = id
)

------解决方案--------------------
SQL code

--1
select * from a
where not exists(select 1 from b where b.id=a.id)
and not exists(select 1 from c where c.id=a.id)

------解决方案--------------------
SQL code

SELECT * FROM A
WHERE NOT EXISTS 
(
SELECT 1 FROM (
SELECT * FROM B
UNION
SELECT * FROM C) AS D WHERE A.id = D.id 
)

------解决方案--------------------
SQL code

declare @t1 table (id int)
insert into @t1
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6

--> 测试数据: @t2
declare @t2 table (id int)
insert into @t2
select 1


--> 测试数据: @t3
declare @t3 table (id int)
insert into @t3
select 3

select * from @t1
except
(select * from @t2
union
select * from @t3)