Skip to content

Instantly share code, notes, and snippets.

@sokil
Created June 19, 2017 08:24
Show Gist options
  • Save sokil/5c66a040889d5ba3cd2f86b499cfe46a to your computer and use it in GitHub Desktop.
Save sokil/5c66a040889d5ba3cd2f86b499cfe46a to your computer and use it in GitHub Desktop.
Clone rows
-- create temporary table
create temporary table cloned_rows_table like source_table;
-- clone some tows to temporary table
insert into cloned_rows_table select * from source_table where some_condition = 42;
-- apply some data modifications to cloned_rows_table
-- ....
-- regenerate auto increment id
SET @max = (select max(id) from source_table);
update cloned_rows_table set id = @max:=@max+1;
-- put cloned rows back to source table
insert into source_table select * from cloned_rows_table;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment