2014-03-12 75 views
0

舉一個例子,下面有兩個表格。如何用MySQL中的單個選擇查詢更新多列?

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

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

我想NewData表更新OldData表。

爲了這個目的,我嘗試使用下面的查詢:

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

但是它給出了一個語法錯誤。

做什麼我嘗試的正確方法是什麼?

+2

這就是爲什麼我在這裏問 –

回答

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

應該'SET o.name = n.name,o.address = n.address'。 –

0

試試這個:

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

這需要2選擇查詢可能性能問題 –

0

試試這個:

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