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

这个语句怎么写?就这点分了
有两个表,假设表table1中字段为姓名、单位、住址、电话,表table2中的字段为姓名、单位,我怎么用最简单效率最高的方法找到table2中姓名、单位字段内容在table1中姓名、单位字段中不一样的?因为表中记录太多,一般的方法太慢,谢谢

------解决方案--------------------
select * from table2 a where not exists(select * from table1 姓名=a.姓名 and 单位=a.单位)
------解决方案--------------------
select table2.姓名,table2.单位,table1.姓名 from table2 left join table1 on table2.姓名 = table1.姓名 and table2.单位 = table1.单位 where table1.姓名 is null
------解决方案--------------------
select *
from table2 a
where not exists(select 1 from table1 姓名=a.姓名 and 单位=a.单位)
同时在table1(姓名、单位)和table2(姓名、单位)上建立索引

------解决方案--------------------
select * from table2 as tmp
where not exists(select 1 from table1 where 姓名=tmp.姓名 and 单位=tmp.单位)