Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: fixing data using other table

Tags:

sql

mysql

I need to fix data in table "tag" using table "tag2",

by match "tag.id" and "tag2.id" and if matched replace "tag2.name" with "tag.name" in "tag" table,

tables structures:

tag:

id     name
1     test
2     test
3     test
4     Tom hancks
5     test
6     amazon
7     car
8     BMW
9     search

tag2:

id     name
1     Google
2     yahoo
3     Microsoft
4     Tom hancks
5     facebook

to return "tag" table like this:

tag:

id     name
1     Google
2     yahoo
3     Microsoft
4     Tom hancks
5     facebook
6     amazon
7     car
8     BMW
9     search
like image 761
mwafi Avatar asked Dec 04 '25 10:12

mwafi


2 Answers

You can do it by using inner join.

 update tag  inner join tag2
 on tag.id = tag2.id     
 set tag.name = tag2.name
like image 98
Danyal Sandeelo Avatar answered Dec 07 '25 10:12

Danyal Sandeelo


Try this:

update tag t1
inner join tag2 on t1.id= t2.id set t1.name=t2.name 
like image 27
Sashi Kant Avatar answered Dec 07 '25 08:12

Sashi Kant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!