日期:2014-05-19  浏览次数:20478 次

找出两个结构相同的表中不同的记录,速度,在线等
如:a、b两个表字段一样,部分资料也一样,
如何找出a中有但是b中没有   或者   b中有而a中没有   的资料

------解决方案--------------------
select * from a
where not exists(select 1 from b where a.主键=b.主键)

select * from b
where not exists(select 1 from a where a.主键=b.主键)
------解决方案--------------------
a中有但是b中没有
select * from a left join b on a.关键字=b.关键字 where b.资料 is null
或者 b中有而a中没有 的资料
select * from a right join b on a.关键字=b.关键字 where a.资料 is null

/*满足你的要求的方法很多,你贴出你的数据及你要的结果
再来看用哪种方法最快最好*/

------解决方案--------------------
select * from 表1 where 主键 not in (select 主键 from 表2)


表1数据多于表2数据

------解决方案--------------------
a中有,b中没有的!
select * from a
where a.主键 not in (select b.主键 from b )

b中有,a中没有的!
select * from b
where b.主键 not in (select a.主键 from a )

------解决方案--------------------
select * from a
minus
select * from b