Easiest way to duplicate rows in a table, subtable, and subsubtable

hari4u2

New Member
I am implementing a "Save As Copy" function for a small web app that uses MySQL.Let's say I have three tables, like so...\[code\]TABLE Doc ID, title, textTABLE DocAttributes ID, DocID -> Doc(ID) title, textTABLE DocSubAttributes DocAttrID -> DocAttributes(ID) title, text\[/code\]What we have here is a situation where a single Document can have multiple DocAttributes, and each DocAttribute can in turn have multiple SubAttributes.Now, if I did not have to worry about the DocSubAttributes table, this would be fairly straightforward, and I would do something like this...\[code\]$insertID = INSERT INTO Doc (title, text) SELECT title, text FROM Doc WHERE ID = $docID;INSERT INTO DocAttributes DocID, title, text SELECT $insertID AS DocID, title, text FROM Doc WHERE ID = $docID;\[/code\]However, because there is a third one-to-many table, this method doesn't work. I need to know the primary keys for each DocAttributes row for creating the corresponding rows in DocSubAttributes.The only way I am aware of to do this is to iterate through DocAttributes one row at a time, selecting all DocSubAttributes with each iteration, and then doing each insert, again one at a time, iteratively.I am wondering: Is there a simpler way to duplicate these rows in their respective tables to create an entirely new and separate data entity without relying on iteration?Thanks for your help
 
Back
Top