1

I've a problem with the following SQL query:

DELETE FROM table1 WHERE uid =
(SELECT table1.uid from table1 INNER JOIN table2 ON table2.user = table1.uid
INNER JOIN table3 ON table3.uid = table2.seminar WHERE table3.end_date < CURDATE()))

The error is: You can't specify target table 'table1' for update in FROM clause

Does anybody have an idea how to fix that?

4

5 回答 5

2

这可能不是最优雅的方式,但您可以将您的条件选择到临时表中,然后从 #MyTempTable 中的位置删除

SELECT table1.uid 
INTO #MyTemp
from table1 INNER JOIN table2 ON table2.user = table1.uid
INNER JOIN table3 ON table3.uid = table2.seminar WHERE table3.end_date < CURDATE())


DELETE FROM table1 
WHERE uid IN
(SELECT uid from #MyTemp)

DROP TABLE #MyTemp
于 2013-09-30T15:41:47.853 回答
1

您可以选择加入表格。

DELETE  table1
FROM    table1 
        INNER JOIN table2 
            ON table2.user = table1.uid
        INNER JOIN table3 
            ON table3.uid = table2.seminar
WHERE   table3.end_date < CURDATE()
于 2013-09-30T15:40:30.627 回答
0

尝试这个:-

DELETE  table1 FROM    table1 INNER JOIN table2 ON table2.user = table1.uid
        INNER JOIN table3 ON table3.uid = table2.seminar
WHERE   table3.end_date < CURDATE()
于 2013-09-30T15:40:42.997 回答
0

我的首选语法是:

Delete deleteAlias
/* Select *         */
from table1 deleteAlias
where exists ( select null from table1 innerTable1 INNER JOIN table2 ON table2.user = innerTable1.uid
INNER JOIN table3 ON table3.uid = table2.seminar WHERE table3.end_date < CURDATE())   

AND deleteAlias.uid = innerTable1.uid

   )

如果这种方法的微妙好处是您可以替换一个简单的“select *”来查看在运行删除之前将删除的内容。

/* Delete deleteAlias */
Select *
from table1 deleteAlias
where exists ( select null from table1 innerTable1 INNER JOIN table2 ON table2.user = innerTable1.uid
INNER JOIN table3 ON table3.uid = table2.seminar WHERE table3.end_date < CURDATE())   

AND deleteAlias.uid = innerTable1.uid

   )
于 2013-09-30T15:43:41.747 回答
0

您不能在 select 子句中以临时模式修改或删除正在使用的表。您可以在这里看到解释和替代方案: MySQL 错误 1093 - 无法在 FROM 子句中指定要更新的目标表

于 2013-09-30T15:44:18.337 回答