0

I need to update one table that looks like the following.

ItemID  FID1    FID2    FID3    FName1  FName2  FName3  
100084  2545    2536    2535    

Using data from another table. The other table looks like:

FID    FName
2545   Name 1
2534   Name 3
2536   Name 2

I want the updated table to look like:

ItemID  FID1    FID2    FID3    FName1  FName2  FName3  
100084  2545    2536    2535    Name 1  Name 2  Name 3

What is a clean way to do this in a single query?

1
  • 2
    You delete the first table. You alter the second table and add ItemID column. Then you pour gasoline and acid on the file that contained the first table so you never make such an atrocity again. Commented Jul 23, 2018 at 20:30

1 Answer 1

1

I won't ask why you want to store your data in a non-normalized form ;-) - But this should work:

update one_table t
left join other_table t1 on t1.FID = t.FID1
left join other_table t2 on t2.FID = t.FID2
left join other_table t3 on t3.FID = t.FID3
set t.FName1 = t1.Fname,
    t.FName2 = t2.Fname,
    t.FName3 = t3.Fname
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Unfortunately I'm inheriting these tables so that's the excuse I'm going with.
After seeing the horrendous performance of this query and the feedback from you and @n-b I was able to get additional time so I could change the tables. Woot!

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.