Merging tables in MySQL - sum up columns

Posted by Alan Williamson on Stack Overflow See other posts from Stack Overflow or by Alan Williamson
Published on 2010-06-15T22:47:43Z Indexed on 2010/06/15 22:52 UTC
Read the original article Hit count: 205

Filed under:
|

I have an interesting problem, that i am sure has a simple answer, but i can't seem to find it in the docs.

I have two separate database tables, on different servers. They are both identical table schema with the same primary keys.

I want to merge the tables together on one server. But, if the row on Server1.Table1 exists in Server2.Table2 then sum up the totals in the columns i specify.

Table1{ column_pk, counter }; "test1", 3 "test2", 4

Table2{ column_pk, counter }; "test1", 5 "test2", 6

So after i merge i want:

"test1",8 "test2",10

Basically i need to do a mysqldump but instead of it kicking out raw INSERT statements, i need to do a INSERT..ON DUPLICATE KEY UPDATE statements.

What are my options?

Appreciate any input, thank you

© Stack Overflow or respective owner

Related posts about mysql

Related posts about mysqldump