怎么用SQL比对两表

来源:百度知道 编辑:UC知道 时间:2024/06/22 10:19:24
表1,A字段包含B字段
A B
111 123
111 235

表2,C字段包含D字段

C D
111 123
如何用SQL来比对,两表之间的差异数据?

--表结构:
CREATE TABLE t1_old (
id int NOT NULL,
log_time DATETIME DEFAULT '') ;
CREATE TABLE t1_new (
id int NOT NULL,
log_time DATETIME DEFAULT '') ;--两表的记录数都为100条。select count(*) from t1_old;select count(*) from t1_new;

Oracle里边可以这样:
select * from 表1 minus select * from 表2

SqlServer2005中可以这样:
select * from 表1 except select * from 表2

sqlserver2000中可以这样:
select * from 表1 where not exists (select 1 from 表2 where C=表1.A and D=表1.B)

sqlserver2000里边的这种方式在Oracle和sqlserver2005中也是可行的

oracle中:

select * from 表1
minus
select * from 表2

SqlServer2000中:

select * from 表1 where not exists(
select * from 表2 where 表2.C=表1.A and 表2.D=表1.B)

---
以上,希望对你有所帮助。

两表字段类型一样~
思路:先把两表的数据合并~,然后把重复的数据剔除掉,剩下的就是两表的差异数据了!
-----实现语句----------------
Select * from (select * from A
Union all
Select * from B) T Gro