MySQL 删除 - 我无法指定目标表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5816840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete - I can't specify target table?
提问by markzzz
Why this query doesn't work?
为什么这个查询不起作用?
DELETE FROM recent_edits
WHERE trackid NOT IN
(SELECT DISTINCT history.trackid
FROM history JOIN recent_edits ON history.trackid=recent_edits.trackid
GROUP BY recent_edits.trackid)
I get this message : "You can't specify target table "recent_edits" for update in FROM clause
我收到此消息:“您无法在 FROM 子句中为更新指定目标表“recent_edits”
回答by Nicola Cossu
Try in this way
用这种方法试试
DELETE FROM recent_edits
WHERE trackid NOT IN
(select * from (SELECT DISTINCT history.trackid
FROM history JOIN recent_edits ON history.trackid=recent_edits.trackid
GROUP BY recent_edits.trackid) as t);
回答by Elysiumplain
You can't post-process a table which is locked for deletion. using the hack select * from (query)
as Nicola states will generate a temporary table instead of direct access.
您不能对已锁定删除的表进行后处理。使用 hackselect * from (query)
作为 Nicola 状态将生成一个临时表而不是直接访问。
Edit - make sure that you give ID to the tables you use since it is nested and will require uniqueID for every table.
编辑 - 确保为您使用的表提供 ID,因为它是嵌套的,并且每个表都需要 uniqueID。