(查询两个表中不相同的记录)关于SQL语句的问题 (100分)

P

PS克

Unregistered / Unconfirmed
GUEST, unregistred user!
A表 字段(tmp) B表 字段(id)
------------------------------------------
AA1 BB1
CC1 DD1
BB1 AA1
-------------------------------------------
要求显示:(把两个表中不相同的记录显示出来)
DD1
CC1
 
select tmp from a where tmp not in(
select a.tmp from a,b where a.tmp=b.id)
union all
select id from b where id not in (
select a.tmp from a,b where a.tmp=b.id)
 
select tmp from a where Not tmp in (select id from b) union (select id from b where Not id in (select tmp from a) )
不知道行不行
 
select atmp from a, b
where a.tmp<>b.id
这样效率高一点.
 
LeeChange的效率高
 
select B.Id as NewId
from A,B
where B.Id <> A.Temp
union
select A.Temp as NewId
from A,B
where A.Temp <> B.Id
 
LeeChange的方法不对吧
 
leeChang的方法应该是可以的吧,只是缺少B表的
 
如果两张表都很大,leechang 的方法在效率上存在一定问题

select tmp into c from a
insert into c select id from b
select * from c group by tmp having count(*)<2
 
用union 或union all!
 
有个苯方法,依次比较,然后显示不同的数据
 
你们有没有试过呀?
LeeChange的方法怎么可行呢?
select a.tmp from a, b //相当于a.tmp 与b.id的笛卡尔集,返回 3*3条记录
where a.tmp<>b.id //去调相等的 AA1,AA1 返回 AA1,BB1等
再从中查询a.tmp,a.tmp中每个记录被返回了n 遍

txdelphi的是对的,任豆豆的not写错了地方
 

Similar threads

S
回复
0
查看
962
SUNSTONE的Delphi笔记
S
S
回复
0
查看
784
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部