How to duplicate easily a record in mysql

I wanted to duplicate a record on my database and the first problem was the primary key.
I couldn’t directly after my INSERT statement put my SELECT because of the primary key..

So here is my solution (theorical):
CREATE TEMPORARY TABLE [your temporary table] SELECT * FROM [the table where you have the row to duplicate] [where statement if you have any];
update [your temporary table] set [your primary key] = «  »;
INSERT INTO [your new table] SELECT * FROM [your temporary table]

In my case it looks like:
CREATE TEMPORARY TABLE temp_tb SELECT * FROM mytable where myid = 12;
update temp_tb set myid = «  »;
INSERT INTO mytable SELECT * FROM temp_tb

On insert you can put another table, in my case I wanted to duplicate a row in the same table.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Time limit is exhausted. Please reload CAPTCHA.