求n个结构相同的表的交集,请各位大大执教sql语句该如何写(100分)

X

xxrxh

Unregistered / Unconfirmed
GUEST, unregistred user!
现有n个结构相同的表,表中的记录有相同的也有不相同的,该如何写sql语句求出交集,即
存在于所有表中的记录,以两个表为例,我写了一下sql语句,但执行不了,请各位
大大指教.sql语句 select NUMB,GZXM from gzxm1 intersect select NUMB,GZXM from gzxm2
 
如果是求n个结构相同的表的交集,可以通过如下方式:
select * from T1,T2,...,Tn
where T1.Field0=T2.Field0 and ... and T1.Fieldn=T2.Fieldn (这里可以根据情况写条件
如果只需要两个条件相同
则只写两个条件即可)
如果是求n个结构相同的表的并集,可以通过如下方式:
select * from T1 where (条件)
union
select * from T2 where (条件)
union ...
union
select * from Tn where (条件)

当然语句如果太长可以用写程序来解决[:)]





 
select * from T1 where (条件)
union
select * from T2 where (条件)
union ...
union
select * from Tn where (条件)

 
同意zgdtxf的做法
select A.NUMB,A.GZXM from gzxm1 A,gzxm2 B
where A.id=B.id
如果多表,可以在这个基础上加。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
467
import
I
I
回复
0
查看
575
import
I
顶部