Posted By

donkeykong on 01/21/14


Tagged

mysql


Versions (?)

MySQL Duplicate Rows With Updates


 / Published in: MySQL
 

URL: http://stackoverflow.com/questions/2783150/mysql-how-to-copy-rows-but-change-a-few-fields

I found this script and tried it. It worked. I thought it was useful for a variety of situations and is easy to follow.

Often times I need nearly duplicated data and don't want to manually cut and paste.

  1. CREATE TABLE temporary_table AS SELECT * FROM sometable WHERE website_id=4;
  2.  
  3. UPDATE temporary_table SET website_id=5;
  4.  
  5. UPDATE temporary_table SET sometable_primary_key_id=NULL;
  6.  
  7. INSERT INTO sometable
  8. SELECT * FROM temporary_table;
  9. DROP TABLE temporary_table;

Report this snippet  

You need to login to post a comment.