我有一个名为GoodsInOld
的表,其中包含4列:
GoodsInOld: Id, CustomerAccount, CustomerName, ItemId
由于新的数据结构,我创建了2个新表来保存数据:
GoodsIn
和GoodsInProduct
。
他们有如下关系:
GoodsIn: Id, CustomerAccount, CustomerName ...
GoodsInProduct: Id, ItemId, GoodsInId ...
GoodsIn
可能有1个或多个GoodsInProduct
我想使用GoodsInOld
或INSERT INTO
将SELECT INTO
中的数据插入到这两个表中?
答案 0 :(得分:2)
insert into GoodsIn (CustomerAccount, CustomerName)
select CustomerAccount, CustomerName
from GoodsInOld
insert into GoodsInProduct(ItemId, GoodsInId)
select o.ItemId, i.id
from GoodsInOld o
inner join GoodsIn i on o.CustomerAccount = i.CustomerAccount
and o.CustomerName = i.CustomerName