2013-02-15 44 views
0

嗨,大家好,我想更新兩個表中的列。例如:abcxyzabc表包含稱爲id,fname,lastnamestatus的字段。並且表xyz包含id,x_idstatus。現在我想要的是通過使用單個查詢從abcstatusxyz更新列狀態。如何更新mysql中多個表的列

我試着這個查詢,但它不工作。

UPDATE abc a1 
JOIN xyz x1 ON a1.id = x1.x_id 
SET a1.status = "D" , 
    x1.status = "delete" 
WHERE a1.id = 15 AND x1.x_id = 15; 

謝謝。

+5

檢查: http://stackoverflow.com/questions/4361774/mysql-update-multiple-tables-與一查詢 和 http://stackoverflow.com/questions/1871537/how-to-update-two-tables-in-one-statement – 2013-02-15 07:56:38

+0

+1用於鏈接相關鏈接 – Khez 2013-02-15 07:58:40

回答

0

編輯的3個表:

UPDATE a1,x1,s1 
SET a1.status = "D" , 
x1.status = "delte", 
s1.status = "D" 
WHERE a1.id = x1.x_id 
AND a1.id = s1.s_id 
AND a1.id = 15; 
+0

由於博揚其工作現在罰款....偉大的工作。 – Akki 2013-02-15 08:17:01

+0

沒問題,我很高興它解決了:) – 2013-02-15 08:23:24

+0

嘿Bojan我們可以用3張桌子做這個嗎? – Akki 2013-02-15 10:23:15

0

如果你的mysql服務器無法更新使用SQL一個查詢兩個表。 您可以使用LOCK TABLES命令,以避免競爭條件:

LOCK TABLES abc WRITE, xyz WRITE; 
update abc set status = "D" where id = 15; 
update xyz set status = "delete" where x_id = 15; 
UNLOCK TABLES; 

問候, 奧馬爾