MySQL Duplicate Function
Sometimes you want the ability to duplicate (clone) entities in mySQL. But duplicating their children can be a huge pain! Here’s how:
/* parent table */
insert into [parent table]
select 0, [field1, field2, field3] from [tablename] where [parent table primary key] = [parent table object ID]
/* child table */
insert into [child table]
select 0, [field1, field2, field3], (select max([parent table primary key]) from [parent table]) from [child table] where [parent table primary key] = [parent table object ID]
important note! make sure that you don’t select the primary key of the table. instead, select 0, and the auto_increment function will automatically figure out the correct primary key
As always, if you have questions, post em in the comments!
July 9th, 2010 at 11:16 am
If you are so smart, why do you keep breaking our website?
July 9th, 2010 at 11:18 am
You look smarter when there are problems to fix ;) when there are no problems that need to be fixed, people assume you don’t actually do anything