2015-10-07 139 views
1

我有以下2個表:更新查詢從2表

table1中是tracsName,tracsid,N,NE...表2是Tracs,kode,N2...。我想用這個代碼改變N值與N2的值,N和更多的是風箭頭

更新查詢,可以更簡單嗎? UPDATE table1 SET N=(select N2 from table2 where tracs='daraname2' AND kode='1-6'), NE=(select NE2 from table2 where tracs='daraname2' AND kode='1-6'), E=(select E2 from table2 where tracs='daraname2' AND kode='1-6'), SE=(select SE2 from table2 where tracs='daraname2' AND kode='1-6'), S=(select S2 from table2 where tracs='daraname2' AND kode='1-6'), SW=(select SW2 from table2 where tracs='daraname2' AND kode='1-6'), W=(select W2 from table2 where tracs='daraname2' AND kode='1-6'), NW=(select NW2 from table2 where tracs='daraname2' AND kode='1-6') WHERE tracsName='daraname2' AND tracsid='1-6'

我有一個錯誤,如果你能給我的邏輯或代碼,它會很好。

+0

哪些SQL? Mysql,sql服務器還是? –

+0

對於mysql我使用mysql front, – Sae

回答

1

這裏是mysql的一般語法:

UPDATE TABLE1 a 
JOIN TABLE2 b ON a.name1 = b.name2 
SET a.n1 = b.n2 

這裏是舉例sql server

UPDATE a 
SET n1 = b.n2  
FROM TABLE1 a 
JOIN TABLE2 b ON a.name1 = b.name2 
+0

謝謝,它非常好,現在我有2臺服務器,備份是不可能的,任何想法如何在2臺服務器上使用mysql代碼。一個在mylocal上,一個在線。 – Sae

+1

https://winashwin.wordpress.com/2012/08/22/mysql-federated-table/ –

+0

非常感謝你 – Sae

1

試試這個代碼

UPDATE table1 

SET  table1.N = table2.n 
FROM table2 WHERE name2 = name1