Change column values based on matching values from another table SQL

Viewed 43

So I have two tables, one with a key and one with rawdata

Key:

ID Zone
1 A
2 B
3 c

Raw Data:

ID DesignatedZone
1
2
2
1
3

I want to fill the empty column in rawdata if the value in the ID columns match.

Desired Output:

ID DesignatedZone
1 A
2 B
2 B
1 A
3 C

Raw Data has 1000+ rows. Would a JOIN be the best way to tackle it or UPDATE? Im also open to an Excel solution as well since I can edit the data before I import it.

This was my guess:

    UPDATE rawdata
    SET rawdata.DesignatedZone = key.Zone
    WHERE rawdata.ID = key.ID
1 Answers

Use an update join:

UPDATE rawdata r
SET r.DesignatedZone = k.Zone
FROM `Key` k
WHERE k.ID = r.ID;
Related