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

求救:关于表内容的复制
表wlw
iname     icount     ncount
t1               4             3
t2               6             1
t3               23           9

表wlw2
iname       count
t1               1
t2               5
t3               14

其中count是icount与ncount的差.

要求:
当表wlw2为空时,就将t1,t2,t3复制到表wlw2中的iname下,同时将t1,t2,t3对应的icount与ncount的差值复制到count字段下.

如上所示。

如果表wlw2中有数据,如下:
iname     count
t2             4
t3             6
t4             7
就将表wlw的数据复制过来,将wlw2的数据替换掉,即:
iname       count
t1               1
t2               5
t3               14

要求用通用的方法实现,谢谢



------解决方案--------------------
truncate table wlw2
insert into wlw2
select iname,icount-ncount from wlw
------解决方案--------------------
delete from wlw2
insert wlw2 select iname,icount-ncount from wlw
------解决方案--------------------
insert into wlw2(iname,count) select iname,icount-ncount
from wlw