mySQL: Updating multiple values in a record with a SELECT from another table
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 开发者_Go百科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.
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';
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
精彩评论