Copying MySQL parent and all related child records to database with new id

Is there an easy way to copy parent records and all related child records using strictly SQL without using cursors or external scripts / code? Here is an example of what I have:

categories
==
category_id
category_name

parent_table
==
parent_record_id
category_id
... <other fields>

child_table1
==
child_table1_id
parent_record_id
... <other fields>

child_table2
==
child_table2_id
parent_record_id
... <other fields>

, . , , , auto_increment. , , category_id. parent_table, - category_id category_id, . parent_record_id child_record1 child_record2.

, , PHP . ?

+5
1

, http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

INSERT INTO table (id, column1, column2) (SELECT NULL, column1, column2 FROM table WHERE whatever_id = 123);
+1

All Articles