如果你的数据库支持minus的话,那很容易了
select * from table1
minus
select * from table2
就能得到table1中存在而table2中不存在的纪录了,反之就能得到table2中存在而table1中不存在的纪录
select * from table1
minus
select * from table2
就能得到table1中存在而table2中不存在的纪录了,反之就能得到table2中存在而table1中不存在的纪录
然后再遍历TABLE2。
where exists ( select * from table2
where table1.pkey = table2.pkey
and( table1.field1 <> table2.field1
or table1.field2 <> table2.field2
...
or table1.fieldn <> table2.fieldn));
from table1 as t1
where not exists(
select *
from table2 as t2
where t1.f1=t2.f1 and t1.f2=t2.f2 and ....
)
union
select * ----在table2但不在table1中
from table2 as t1
where not exists(
select *
from table1 as t2
where t1.f1=t2.f1 and t1.f2=t2.f2 and ....
)
http://www.itpub.net/showthread.php?s=&threadid=21181
还有我的两个表中对应的字段的字段名不同这个minus可能不好用吧?
我的想法是在程序中比较
取出两个结果集,按主键排序,然后从两个结果集的第一条纪录开始比较
主键,若主键相同则再比较其他字段的内容,若主键不同,则根据两个主键
的大小来判断两个表中两条纪录的关系。这个想法应该可以实现的,就是
不知道效率如何?望大家指教!
楼上的sql语句好像可以呀,不知道效率如何,试试先!
顶上去!