开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜