Link to home
Start Free TrialLog in
Avatar of Mike and Stephie MacDonald
Mike and Stephie MacDonald

asked on

MySQL Best way to duplicate rows from tables and subtables

A MySQL  database has three  levels of subtable.
project->snapshot->layer  

Need to duplicate a project record along with all subrecords from snapshot and layer
a project can have many snapshots, a snapshot can have many layers

Would prefer to do this in PHP but stored procedure is OK
All comments and advice welcome and appreciated
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

First, query all records and then insert them.
project->snapshot->layer

@NewID = ??? find new id somehow ???;

insert into projects
select @NewID, col1, col2, col3,... from project where ID=@OldID;

insert into snapshot
select @NewID, col1, col2, col3,... from snapshot where PrjID=@OldID;

Open in new window


for the 3rd table, we need to know your db diagram / relations / column names...
SOLUTION
Avatar of Mike and Stephie MacDonald
Mike and Stephie MacDonald

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike and Stephie MacDonald
Mike and Stephie MacDonald

ASKER

Brilliant HainKurt - thanks very much for the detail. Will study and learn.
Thanks also Mukesh