0

I have two tables (let's call them A and B) with same structure and i need to synchronize data in them...

There's one primary key field, with same value in both tables, and several fields with value in table A and null (or obsolete value that need to be replaced with current value from table A) in table B... I need to copy value from table A to table B.

Is there any easy way (other than replication) to do this in mySQL 4.1?

thanks in advance

4
  • JOIN 2 tables on the primary key and issue an UPDATE query? Commented Jan 11, 2012 at 13:07
  • use LEFT JOIN to identify your NULL records in the second table then use dev.mysql.com/doc/refman/4.1/en/insert-select.html as answered below Commented Jan 11, 2012 at 13:34
  • @SergeyBenner actually, i made an error in question... some fields have null value, some has obsolete value that need to be replaced so your answer won't work, but thanks anyway.... Commented Jan 11, 2012 at 13:55
  • 2
    Check INSERT IGNORE or INSERT...ON DUPLICATE KEY UPDATE statements for your merging needs. Check also dev.mysql.com/doc/refman/4.1/en/replace.html REPLACE INTO Commented Jan 11, 2012 at 14:02

2 Answers 2

2

Try this -

UPDATE table_b b, table_a a 
SET b.field1 = a.field1, b.field2 = a.field2  
WHERE b.primary_key = a.primary_key 

add the fields as required.

Sign up to request clarification or add additional context in comments.

Comments

0

Can you just do:

INSERT INTO table1 (field1,field2,field3) SELECT field1,field2,field3 FROM table2;

Or do you actually already have data in table2 and you need to update it rather than insert new columns?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.