读书人

请问啊查询A表中未曾出现在B表和C表中

发布时间: 2012-09-13 09:51:53 作者: rapoo

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

SQL code
id   001 002003004005006 

B表
SQL code
002003005

C表
SQL code
003004005

查询结果
001
006


[解决办法]
SQL code
select * from Awhere not exists(  select * from B where A.id = id)andnot exists(  select * from C where A.id = id)
[解决办法]
SQL code
--1select * from awhere 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 AWHERE NOT EXISTS (SELECT 1 FROM (SELECT * FROM BUNIONSELECT * FROM C) AS D WHERE A.id = D.id )
[解决办法]
SQL code
declare @t1 table (id int)insert into @t1select 1 union allselect 2 union allselect 3 union allselect 4 union allselect 5 union allselect 6--> 测试数据: @t2declare @t2 table (id int)insert into @t2select 1--> 测试数据: @t3declare @t3 table (id int)insert into @t3select 3select * from @t1except(select * from @t2unionselect * from @t3) 

读书人网 >SQL Server

热点推荐