Is there a way to use a MySQL INSERT similar to the following:
INSERT INTO doc_details SELECT * FROM doc_details WHERE dd_id = 1
This doesn’t work because the primary key is being repeated and it can get very long-winded expanding the columns out.
The purpose of this is to duplicate rows in the same table which will get modified later, retrieving the last_insert_id for the new record. So ideas for other ways to do this would be appreciated too.
Thanks.
Thanks for the answers. Really appreciated. Because most answers specify the column, this led to some extra research that said ‘wildcards cannot be used in
INSERTstatements. Select, Modify and insert into the same tableI managed to solve this in my application with a separate
SELECTthen theINSERTwith the columns expanded with a Perlmapfunction:SELECT * FROM doc_details WHERE dd_id = 1Then in Perl, with the row as a hash reference in $data:
$data->{'dd_id'} = 0;$columns = join(',', map {$_ .'='. $dbh->quote( $data->{$_} ) } keys %{$cdh} );
Does the trick nicely – it copies the row regardless of changes to the column structure/order as long as the auto_increment column is maintained.
I know it’s not a pure SQL solution – although Ravinder provided one that was.
Thanks to all!