tags:

views:

186

answers:

1

I'm trying to update one table based on values in another table. What's wrong with the following request? Error: Unknown column 'source.col3' in 'where clause'

UPDATE target 
  SET target.col1 = source.col1 * target.col2,
WHERE target.col3 = source.col3
+2  A: 

Well, for one you're not specifying 'source' as a table anywhere.

MySQL actually supports multiple table update, so you could write your code as:

UPDATE target, source
   SET target.col1=source.col1*target.col2,
 WHERE target.col3=source.col3

Now whether that would actually do what you want I can't tell without knowing more about your tables.

ChssPly76
hmmm seems like a pretty silly question :) I was actually slipping up on a bit I removed from the SQL at the end, LIMIT 1. It said inappropriate use of UPDATE and LIMIT
Peter
`LIMIT` only works for single table `UPDATE`, not for multiple tables. The reason for that is you could be updating more than one table at once in which case it's impossible to apply limit consistently. For example, imagine that you added `set ..., source.col4 = target.col4 where ... limit 1` to the above - you're now updating both tables 2 rows at a time (one from each). How should `limit 1` be handled?
ChssPly76