1. 程式人生 > >MySQL:You can’t specify target table ‘A’ for update in FROM clause

MySQL:You can’t specify target table ‘A’ for update in FROM clause

按照MYSQL5.0文件的解釋:我們不能在修改表A的同時在其子查詢中使用到表A,但是可以通過在子查詢中在巢狀一層針對表A的子查詢,因為最裡層的子查詢產生的結果存在臨時表中,與表A沒有關係。

解決方法:把類似於 UPDATE t ... WHERE col = (SELECT ... FROM t ...); 改寫成UPDATE t ... WHERE col = (SELECT (SELECT ... FROM t...) AS _t ...);即可。

文件原文:In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms:

例如 UPDATE student SET student.name = 'aa' WHERE id=(SELECT __tmpTable.id FROM (SELECT...FROM t) as _tmpTable )