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

关于数据库表导入的问题,请高手们指教,在线等。。。。。。。。
我有两个新闻表news1和news2,news1和news2的区别是:news1里面有两个字段c1和l1,而news2里面有两个字段是cc1,ll2,他们所表示的字段意义是相同的,只是名字不同,他们的主键都是id,news1里面的内容多于news2里面的内容,我现在向把news1里面多于news2的字段内容导入到news2表中,将如何实现呢??

------解决方案--------------------
insert into news2(cc1,ll2)
select c1,l1
from news1
where not exists(select 1 from news2 b where news1.c1=b.cc1 and news1.l1=b.ll2)
------解决方案--------------------
如果两表是通过ID关联

insert into news2(cc1,ll2)
select c1,l1
from news1
where id not in (select id from news2)
------解决方案--------------------
insert news2
select * from news1 where not exists(select 1 from news1 a inner join news2 b on a.id=b.id)
------解决方案--------------------
insert into news2(id,cc1,ll2)
select id,c1,l1
from news1
where not exists(select 1 from news2 b where news1.id=b.id)