首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问一条SQL 写法

2012-02-14 
请教一条SQL 写法就是将a表有的数据但b表没有的数据插入到b表insertintobselect*fromawhereidnotin(select

请教一条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 left Join b On a.id = b.id Where b.id is null
[解决办法]
insert into b
select * from a
where not exists(select 1 from b where b.id=a.id)

在b.id上建索引
[解决办法]
insert into b
select * from a as tmp
where not exists(select 1 from b where id=tmp.id)

[解决办法]
同意paoluo(一天到晚游泳的鱼)的想法,这个通过左连接的一般比not in与not exists效率高,但是如果想下率更高,可以在gahade(与君共勉) 所有的方式建立索引,两个表的id都建立索引,然后用paoluo(一天到晚游泳的鱼)的:insert into b select A.* from a left Join b On a.id = b.id Where b.id is null

[解决办法]
左连接的一般比not in与not exists效率高??
[解决办法]
insert into b select a.* from a where a.id not in(select distinct b.id from b)
[解决办法]
左连接的一般比not in与not exists效率高??

我不同意这种说法,exists 通常比 in / like 更快.
最好用exists 再在ID上建索引
[解决办法]
同意free_pop2k() ( )的说法

热点排行