[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2321
  • Last Modified:

subquery? Update column in one table with value from another table

Using MySQL, I am trying to update one column in Table1 with the value from another column in  Table2. I guess I need a subquery, but I am not sure how to go about it.  Previously, there was not an autoincrement primary key in the Table2. Instead, the primarykey was derived from meta data. So, to fix this problem, I added an autoincrement primary key to each Table2 record. But now I have to update each Table1 record with the new primary key from Table2. The match can be the OLD primary key (the derived one), which is in both tables. Note, the old faux primary key is a VARCHAR, not an integer, if that matters.
0
aberns
Asked:
aberns
1 Solution
 
mankowitzCommented:
I think this is what you want. Just in case, it is always a good idea to back up your data first.

update table1,table2
set table1.key=table2.key
where table1.oldkey=table2.oldkey
0
 
SandeepratanCommented:
-- THIS IS WHAT U NEED

update T1
set T1.key=T2.key
FROM  table1 T1 Inner Join table2 T2 on T1.oldkey=T2.oldkey
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now