How to merge two tables together with same number of rows corresponding to same id numbers?

Issue

MySQL: How to merge two tables together with same number of rows corresponding to same id numbers? Can someone please help me with writing a query for this. I wrote this:

INSERT INTO table1.code
SELECT code FROM table2
WHERE table1.id = table2.id

But, I am getting mysql error: #1054 - Unknown column 'table1.id' in 'where clause'

Table 1

id name code
1 abc
2 def
3 ghi

Table 2

id code
1 12
2 Ab
3 D2

Required MYSQL DB Table 1

id name code
1 abc 12
2 def Ab
3 ghi D2

Solution

It looks like you simply need to join your two tables and update table1

update t1
join t2 on t1.id = t2.id
set t1.code = t2.code

Answered By – Stu

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published