Mysql Duplicate and Modify Row

I was trying to find an easy way to duplicate a table row in mysql, and it appears that mysql can handle that pretty easily.  Just Use:

insert INTO `server`.`ad` (`adgrp`,`adname`,`ad`,`adlink`,`adtype`,`adprovider`,`visible`) SELECT `adgrp`,`adname`,`ad`,`adlink`,`adtype`,`adprovider`,`visible` FROM `server`.ad where idad=46;

to duplicate any row on any table, or use the  CONCAT() function to add text to the newly created row. In my case i needed to set the name just to be “_copy” of the original one:

insert INTO `server`.`ad` (`adgrp`,`adname`,`ad`,`adlink`,`adtype`,`adprovider`,`visible`) SELECT `adgrp`,CONCAT(`adname`,"_COPY"),`ad`,`adlink`,`adtype`,`adprovider`,`visible` FROM `server`.ad where idad=46;

In the case above the idad column is of course the primary key and auto-increment, so it cannot be copied.

Leave a Reply