我遇到以下SQL查询的问题:
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()))
错误是: 您不能在FROM子句
中为更新指定目标表'table1'有人知道如何解决这个问题吗?
答案 0 :(得分: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
答案 1 :(得分: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()
答案 2 :(得分: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()
答案 3 :(得分: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
)
答案 4 :(得分:0)
您无法在select子句中修改或删除临时模式下使用的表。 你可以在这里看到解释和替代方案: MySQL Error 1093 - Can't specify target table for update in FROM clause