日期:2014-05-18  浏览次数:20512 次

急! 比较两个相同结构的table的不同数据.
有两个相同结构的数据表:
Tb1,Tb2
结构为:
ID,Name,address

数据:
tb1
ID     Name         Address
1       zhang       bj
1       wang         bj

tb2:
ID     Name         Address
1       zhang       bj
1       wang         sh
1       song         bj

想得到的结果是:
字段名   原值     新值
ID           bj         sh
ID           1           1
Name       song     song
address   bj       bj


------解决方案--------------------
确认下 结果 有没贴错

或者解释下 结果 怎么来的

------解决方案--------------------
结果的原值是怎么来的,还有下面两条也不一样阿?怎么不显示

tb1的结果
1 wang bj
tb2的结果
1 wang sh
------解决方案--------------------
---楼主的显示不正确哟。。以下方法对比
select [表]='t1',* from t1 where binary_checksum(*) not in(select binary_checksum(*) from t2)
union all
select '表'='t2',* from t2 where binary_checksum(*) not in(select binary_checksum(*) from t1)

-----------
05用except
select * from t1
except
select * from t2

union all
select * from t2
except
select * from t1

----------或
select * from t1 where not exists(select 1 from t2 where ID =t1.ID and Name =t1.Name and Address =t1.Address)
union all 
select * from t2 where not exists(select 1 from t1 where ID =t1.ID and Name =t1.Name and Address =t1.Address)
)

------解决方案--------------------
结果有两个 ID ?
------解决方案--------------------
不会,帮顶