insert into table select * from table where primarykey=1
I just want to copy one row to insert into the same table (i.e., I want to duplicate an ex
Here's an answer I found online at this site Describes how to do the above1 You can find the answer at the bottom of the page. Basically, what you do is copy the row to be copied to a temporary table held in memory. You then change the Primary Key number using update. You then re-insert it into the target table. You then drop the table.
This is the code for it:
CREATE TEMPORARY TABLE
rescueteamENGINE=MEMORY SELECT * FROMfitnessreport4WHERE rID=1;# 1 row affected. UPDATErescueteamSET rID=Null WHERE rID=1;# 1 row affected.INSERT INTOfitnessreport4SELECT * FROMrescueteam;# 1 row affected. DROP TABLErescueteam# MySQL returned an empty result set (i.e. zero
rows).
I created the temporary table rescueteam. I copied the row from my original table fitnessreport4. I then set the primary key for the row in the temporary table to null so that I can copy it back to the original table without getting a Duplicate Key error. I tried this code yesterday evening and it worked.