Shun Zu
Shun Zu

Reputation: 119

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

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

Upvotes: 1

Views: 192

Answers (1)

Stu
Stu

Reputation: 32609

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

Upvotes: 1

Related Questions