Mar 3rd

MySQL copy a row from one table to another.

Recently on a project it became necessary to copy data from one database table to another, identical table. The theory is that once data is data, it is data forever. Meaning this; Just because it isn’t useful information at the moment doesn’t necessarily mean that it will always be useless. Long story short, if you have data… Keep it.

Of course it is possible to just SELECT the data from table_1, INSERT it into table_2 then DELETE the original record from table_1, but why do that when MySQL has built in a shortcut? Enter, the INSERT/SELECT hybrid query.

INSERT INTO table_2
SELECT * FROM table_1 t1
WHERE ( t1.id_field = 'some_unique_id' );

After that, just follow it up with a standard DELETE query to remove the original record.

DELETE FROM table_1
WHERE ( id = 'some_unique_id' );
  1. Lennert says:

    I’ve searched many websites for this, and this WORKS!
    THANKS!

  2. I’m glad I could help, nice to know the code gets put to use.

Leave a Reply