I have a table “test” with an auto incremented id and an arbitrary number of columns.
I want to make a copy of a row in this table with all columns the same except for the id of course.
Is there a way to do this without naming all columns?
I thought INSERT... SELECT... ON DUPLICATE KEY would help me until I realised that it never makes an INSERT ON DUPLICATE, it just updates the existing row.
Let us say your table has following fields:
then, to copy values from one row to the other row with new key value,
following query may help
This will generate a new value for
pk_idfield and copy values fromcol1, andcol2of the selected row.You can extend this sample to apply for more fields in the table.
UPDATE:
In due respect to the comments from JohnP and Martin –
We can use temporary table to buffer first from main table and use it to copy to main table again.
Mere update of pk reference field in temp table will not help as it might already be present in the main table. Instead we can drop the pk field from the temp table and copy all other to the main table.
With reference to the answer by Tim Ruehsen in the referred posting: