且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 MySQL 中,如何复制表中的现有记录和依赖于父表 ID 的支持表?

更新时间:2023-02-03 23:11:33

在 Core 和 Activity 中复制记录很简单

Duplicating the records in Core and Activity is straight forward

而且可能看起来像这样:

And would probably look something like this:

set @sourceCoreID = 12;

insert into core (coreLabel)
  select coreLabel
  from core
  where coreID = @sourceCoreID;

set @newCoreID = last_insert_id();

insert into activity (coreID, menuID)
  select @newCoreID, menuID
  from activity
  where coreID = @sourceCoreID;

您现在需要做的就是将一些数据从新的 activity 行复制到 steps(至少在您的示例结果中看起来是这样).而且不需要太多魔法:

All you need now, is to copy some data from the new activity rows to steps (At least it looks like that in your sample result). And there is not much magic needed:

insert into steps(coreID, aID)
  select coreID, aID
  from activity
  where coreID = @newCoreID;

db-fiddle