SQL query: how do I change a value according to a lookup table?

Go To StackoverFlow.com

6

[update: I am using MySQL 4.1.25 ]

I think this must be a simple thing to do, but I'm a SQL noob and need some help. I have a lookup table that is something like:

lookup_table

key1, value1
key2, value2
key3, value3
...
keyN, valueN

Then I have another table that has a random list of the keys (with repeats) in one column and I need to add the associated values in the corresponding column.

For example, the second table might be:

second_table

key3, ?
key1, ?
key1, ?
key40, ?

I need to replace the ?s in second_table with the values from lookup_table as follows:

second_table (updated)

key3, value3
key1, value1
key1, value1
key40, value40

This seems like something that a simple SQL query should address. Any suggestions?

2009-06-16 14:57
by Greg
can you state which sql platform you are using as well - thiswayup 2009-06-16 15:07
@Greg: why denormalize in this way. Clue's in the name: rather than redundantly storing the value in secondtable, look up the value in lookuptable? This prevents anomalies e.g. what if value1 was changed to something else in lookuptable but not in secondtable - onedaywhen 2009-06-16 15:20
@onedaywhen: Good point, but this is part of a migration from one system to another. The "key" fields in "second_table" will ultimately be dropped - Greg 2009-06-17 01:11


8

I much prefer the following syntax when updating with a join (instead of doing a subquery). It allows you to see results before you update them and know the query's right.

select
   st.key,
   lt.value
--update st set value = lt.value
from
   second_table st
   inner join lookup_table lt on
       st.key = lt.key

Note that when you're ready to update, select everything from update on down.

Update: Thanks to tekBlues, I've found out that the above works on SQL Server and Oracle, at the very least. MySQL has a bit different syntax:

update
    second_table st
    inner join lookup_table lt on
        st.key = lt.key
set
    st.value = lt.value

Those are the big RDBMS's, so hopefully one of those is helpful.

2009-06-16 15:02
by Eric
This syntax, as far as I know, is not available in all database engine - tekBlues 2009-06-16 15:04
@tekBlues: I was under the impression that was ANSI. What DB's isn't this viable in? If that's the case, I want to stop recommending it - Eric 2009-06-16 15:05
Anyway, when using SQL Server I prefer it to - tekBlues 2009-06-16 15:06
Eric, at least in INFORMIX you can do it, I'm sure of that. It a royal pain, because when you wanna do an update to more that one field you have to use this horrible syntax: UPDATE table SET (a,b) = ((select.. - tekBlues 2009-06-16 15:08
Sorry. I mean't in INFORMIX you can't use the UPDATE .. FRO - tekBlues 2009-06-16 15:08
@Eric & tekBlues: thanks for the advice. I tried the second approach and it worked like a charm. Now I just need to figure out exactly why it works. ;- - Greg 2009-06-17 01:44


6

Along with the other answers, you could also accomplish this with a join...

UPDATE second_table
SET    value = L.value
FROM   second_table S join lookup_table L on S.key = L.key
2009-06-16 15:01
by Scott Ivey


5

update second_table 
set value = 
(select value from lookup_Table
where lookup_table.key = second_table.key)
2009-06-16 14:59
by tekBlues
@tekBlues: Thanks for this. I think it would work if the select statement produced a single unique row, but since it returns multiple rows the set statement throws an error. The join from Eric handles multiple rows though - Greg 2009-06-17 01:47
Ads