You basically need to keep track the new and old id from `mo_sms` 1. load all DB2.mo_sms that you want to copy 2. for each row in this result set 1. store the actual DB2.mo_sms id 2. save this row on DB1.mo_sms 3. store the new DB1.mo_sms id, you can use [LAST_INSERT_ID()][1] 4. load all DB2.mt_sms entries related to id that you stored on step 2.1 5. save all entries on DB1.mt_sms changing the mt_sms.id_MO to the new id received on DB1 (the one which you stored on 2.3) This should work. [1]: http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id