当前位置: 代码迷 >> ASP.NET >> 请问一条SQL 写法
  详细解决方案

请问一条SQL 写法

热度:1695   发布时间:2013-02-26 00:00:00.0
请教一条SQL 写法
就是将a表有的数据但b表没有的数据插入到b表
insert   into   b   select   *   from   a   where   id   not   in(select   id   from   b)  
请问   这在SQL     上有更好的优化吗?

在线等     急~~!!!!

------解决方案--------------------------------------------------------
insert into b select a.* from a join b on b.id <> a .id

没在查询分析器里跑;错了别骂我啊

期待高人的好方法
------解决方案--------------------------------------------------------
insert into bselect a.* from a left join b on a.id = b.idwhere b.id is null
------解决方案--------------------------------------------------------
exists效率要高一些
-------------------------------------------------------
insert into 表B

select * from 表a a
where not exists (select 1 from 表B where id=a.id)
------解决方案--------------------------------------------------------
leohuang 顶
------解决方案--------------------------------------------------------
up
------解决方案--------------------------------------------------------
个人感觉exists效率糕点
------解决方案--------------------------------------------------------
not in 的效率不好
not exists 就不了解了
经常用not exists来判断是否存在某一条记录是否存在
------解决方案--------------------------------------------------------
要查询分析器中看一下执行计划就知道了
------解决方案--------------------------------------------------------
試試這個

insert into b select A.* from a left Join b On a.id = b.id Where b.id is null
  相关解决方案