如何在MySQL中使用单个选择查询更新多个列?

时间:2014-03-12 06:14:46

标签: mysql sql database

举个例子,下面有两个表。

OldData
-----------
    id
    name
    address

NewData
-----------
    nid
    name
    address

我想用OldData表更新NewData表。

为此,我尝试使用以下查询:

UPDATE OldData SET (name, address) = (SELECT name, address FROM NewData WHERE nid = 234)
WHERE id = 123

但它给出了语法错误。

我尝试做什么的正确方法是什么?

3 个答案:

答案 0 :(得分:3)

UPDATE OldData o, NewData n 
SET n.name = o.name, n.address = o.address 
where n.nid=234 and o.id=123;

答案 1 :(得分:0)

试试这个:

Update oldData set name = (select name from newData where nid = 234),address = (select address from newData where nid = 123);

答案 2 :(得分:0)

试试这个:

UPDATE OldData a,NewData b 
SET a.name = b.name , a.address = b.address 
WHERE a.id=123 AND b.nid = 234