sql问题

qqyatou 2010-11-22 02:22:38
说明下,上面A表的数据是从B表里来的,现在想从B表里查出A表里没有的数据,我写了个sql 但是效率非常低,数据多的时候就死翘翘了,
想请教下 还有什么好的方法没有,
先谢谢了


create table A(
a varchar(50),
b varchar(50),
c varchar(50),
d varchar(50),
e varchar(50),
f varchar(100)

)
insert into A(a,b,c,d,e,f) values('1','1','3','4','aaaa','111111');
insert into A(a,b,c,d,e,f) values('3','2','6','7','bbbb','222222');
insert into A(a,b,c,d,e,f) values('6','4','7','3','cccc','333333');


create table B(
a varchar(50),
b varchar(50),
c varchar(50),
d varchar(50),
e varchar(50)
)
insert into B(a,b,c,d,e) values('1','1','3','4','aaaa');
insert into B(a,b,c,d,e) values('3','2','6','7','bbbb');
insert into B(a,b,c,d,e) values('6','4','7','3','cccc');
insert into B(a,b,c,d,e) values('3','5','2','1','dddd');
insert into B(a,b,c,d,e) values('4','5','3','1','eeee');
insert into B(a,b,c,d,e) values('6','5','5','5','fffff');
insert into B(a,b,c,d,e) values('7','6','2','8','rrrrr');

select * from A

select * from B

select * from B
where
a not in (select a from A)
or b not in (select b from A)
or c not in (select c from A)
or d not in (select d from A)


drop table A
drop table B


...全文
51 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoyefeng1022 2010-11-22
  • 打赏
  • 举报
回复
各显神通!2楼好!学习了!
fpzgm 2010-11-22
  • 打赏
  • 举报
回复

select * from B
where not exists
(select 1 from A where A.a=B.a and A.b=B.b and A.c=B.c and A.d=B.d and A.e=B.e)
王向飞 2010-11-22
  • 打赏
  • 举报
回复
select a,b,c,d,e from B
except
select a,b,c,d,e from A
-晴天 2010-11-22
  • 打赏
  • 举报
回复
select * from b
where not exists(select 1 from a where a=b.a or b=b.b or c=b.c or d=b.d)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧