2016-03-10 85 views
1

這裏多個表的列是我的代碼:一個更新一次

UPDATE 
tb_character_name t1, tb_character_area_crime t2, tb_character_friend t3, 
tb_character_addr t4, tb_character_parents t5, tb_character_gps t6, 
tb_character_image t7, tb_character_info t8, tb_character_data t9 
SET 
t1.item_deleted = 1, t2.item_deleted = 1, t3.item_deleted = 1, 
t4.item_deleted = 1, t5.item_deleted = 1, t6.item_deleted = 1, 
t7.item_deleted = 1, t8.item_deleted = 1, t9.item_deleted = 1 
WHERE 
t1.id_character = '123456' AND t2.id_character = t1.id_character AND t3.id_character = t2.id_character AND 
t4.id_character = t3.id_character AND t5.id_character = t4.id_character AND t6.id_character = t5.id_character AND 
t7.id_character = t6.id_character AND t8.id_character = t7.id_character AND t9.id_character = t8.id_character; 

此代碼正常運行,但列(item_deleted)沒有更改爲1。我不想打電話9個的MySQL查詢因爲我知道它的CPU很貴。這段代碼有什麼問題?

+1

連接9個表格的一次更新將比9更簡單的更新「便宜」 ;我真的很驚訝,如果這個怪物是更有效的任何方式。我的猜測是什麼是錯的是,這9個表中沒有一個id_character字段與指定的值 – Uueerdo

+0

我試着改變操作rator和OR,但沒有區別。並且,是的,所有表都將id_character字段設置爲0 –

+0

如果它們都是id_character = 0,那麼爲什麼您會期待使用WHERE id_character ='123456''的查詢來影響其中的任何一個? ...和OR甚至會更加災難性。清楚的是,你的查詢並不表示試圖並行更新這些表,它是一個9表連接。 – Uueerdo

回答

1

您可以使用各種JOIN方法更新多個表的列,只要您爲每個此類字段指定哪個表(實際上在這種不明確性的情況下需要這些表),那麼這些列可以具有匹配的名稱但是,對於使用相同標準的大量表格,單獨查詢可能更快,更穩定/可預測,並且更連貫。

+0

謝謝你的反饋。 –