如果您不关心重复项,则可以从DB1获得最大的ID,并将其添加到DB2中的每个ID。但是,您说的
name可能是唯一的,所以让我们做对吧。
我假设所有
id列都是
INTEGER PRIMARY KEY,即自动递增。
打开DB1,并附加DB2:
ATTACH '...' AS db2;
复制DB1中尚不存在的所有水果和果汁(这些将获得新的ID):
INSERT INTO Fruit(name)SELECt nameFROM db2.FruitWHERe name NOT IN (SELECt name FROM Fruit);INSERT INTO Juice(name)SELECt nameFROM db2.JuiceWHERe name NOT IN (SELECt name FROM Juice);
现在,复制配方,同时通过相应的名称查找新的ID值:
INSERT INTO Recipe(juice_id, fruit_id)SELECt (SELECT id FROM Juice WHERe name = (SELECt nameFROM db2.JuiceWHERe id = Recipe2.juice_id)), (SELECt id FROM Fruit WHERe name = (SELECt nameFROM db2.FruitWHERe id = Recipe2.fruit_id))FROM db2.Recipe AS Recipe2;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)