views:

119

answers:

2

Trying to sort out the correct syntax for this UPDATE:

UPDATE `foo` 
   SET (`x`, `y`, `z`) = (SELECT `x`, `y`, `z` 
                            FROM `bar` 
                           WHERE `id` = 'baz');

In the actual query, there are 165 columns so I very much do not want to have to do x = x for each column.

The columns are not a perfect match so SELECT * is not an option.

+1  A: 

In MySQL you can add multiple tables to an UPDATE like this:

UPDATE `foo`, `bar`
SET `foo`.`x` = `bar`.`x`, 
    `foo`.`y` = `bar`.`y`, 
    `foo`.`z` = `bar`.`z`
WHERE `id` = 'baz';
Matijs
Thank you for the reply! I made a test like so: UPDATE `test` SET `test`.`unique_id` = `store_import`.`unique_id`,`test`.`synced_on` = `store_import`.`synced_on`FROM `store_import` WHERE `store_import`.`entry_id` = 137This gives error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `store_import` WHERE `store_import`.`entry_id` = 137' at line 4 Can you see where the problem lies?
jerrygarciuh
Yeah I already changed the solution since I posted it. I mixed up MySQL and MSSQL. The current version is the one. :-)
Matijs
Ah well. Thanks. Will take a long time getting all 165 columns into query!
jerrygarciuh
You may want to alias the tables to speed up your input.UPDATE foo a, bar bSET a.x=b.x, a.y=b.y ......
MindStalker
+1  A: 

You are trying to update items in foo where foo.id = bar.baz?

UPDATE foo JOIN bar
SET foo.x=bar.x, foo.y=bar.y
WHERE foo.id=bar.baz
MindStalker
Note JOIN is the same as , in list of table.
MindStalker