How to move records between mysql databases

Moving a db record from on database to the same table in another database is simple:


REPLACE INTO new_database.tableName SELECT * FROM old_database.tableName


If you need to get a bit more complex and extract only certain records from one table and move them to another database:


REPLACE INTO new_database.tableName
SELECT * FROM old_database.tableName WHERE id in (1,33,223,555 ...)


Or, you can do a subselect, to get child records from a foreign key


REPLACE INTO new_database.tableName
SELECT * FROM old_database.tableName WHERE your_foreign_key_column in (1,33,223,555 ...)


One level deeper


REPLACE INTO new_database.tableName
SELECT * FROM old_database.tableName WHERE your_foreign_key_column in
(SELECT id FROM old_database.foreign_tableName WHERE id=222)

No comments:

Post a Comment