views:

32

answers:

1

I'm trying come up with the best method of synchronizing particular rows of 2 different database tables. So, for example there's 2 product tables in different databases as such...

Origin Database

product{
    merchant_id,
    product_id,
    ... additional fields
}

Destination Database

product{
    merchant_id
    product_id
    ... additional fields
}

So, the database schema is the same for both. However I'm looking to select records with a particular merchant_id, remove all records from the destination table that have that merchant_id and replace those records with records from the origin database of the same merchant_id.

My first thought was using mysqldump, parsing out the create table statements, and only running the Insert Statements. Seems like a pain though. So I was wondering if there is a better technique to do this.

I would think mysql has some method of creating INSERT statements as output from a SELECT statement, so you can define how to insert specific record information into a new db.

Any help would be appreciated, thank you much.

+1  A: 

phpMyAdmin has part of this capability: You can run a query and then export the results of that query into a file containing CREATE statements.

Update: And mysqldump has it too: Link

mysqldump -u username -p --where="id='merchant_id'" databasename

In regards to replacing merchant IDs, that part I don't entirely understand yet. You may be better off doing a manual search+replace on them. Can you make a real life example of two such records?

Pekka
Yeah I know mysqldump has that functionality, I was looking for a slicker solution, other wise I have to parse through file to remove all CREATE TABLE statements. I'm was thinking there might be a method to only get the INSERT statements.
Aglystas
@Aglystas Why not remove the CREATE TABLEs using `--no-create-info`? http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-info
Pekka