0

伙计们,我正在尝试使用嵌套查询从数据库中删除一些记录:

delete from imageviewer_crreviewerformdata
where imageviewer_crreviewerformdata.FormDataId in
(select distinct imageviewer_crreviewerformdata.FormDataId
from imageviewer_crreviewerformdata,imageviewer_crtask
where imageviewer_crreviewerformdata.TaskId = imageviewer_crtask.TaskId
and imageviewer_crtask.RevProtId in (1));

我得到了众所周知的mysql异常。有人可以帮助制定查询吗?

4

3 回答 3

1
delete d 
from imageviewer_crreviewerformdata d
inner join imageviewer_crtask t on d.TaskId = t.TaskId
where t.RevProtId in (1)
于 2013-10-22T10:51:45.363 回答
0

我认为它只是告诉你从查询中删除表别名

delete from imageviewer_crreviewerformdata
where FormDataId in
(select distinct imageviewer_crreviewerformdata.FormDataId
from imageviewer_crreviewerformdata,imageviewer_crtask
where imageviewer_crreviewerformdata.TaskId = imageviewer_crtask.TaskId
and imageviewer_crtask.RevProtId in (1));
于 2013-10-22T10:51:01.910 回答
0

当我需要使用该表作为选择源从表中删除时,我通常会创建一个临时表作为删除参考。也许它有点传统,但它有效。

您需要创建一个临时表,通过加入临时表删除,然后删除临时表。

CREATE TEMPORARY TABLE temp.tempdelete AS select distinct
imageviewer_crreviewerformdata.FormDataId
from imageviewer_crreviewerformdata,imageviewer_crtask
where imageviewer_crreviewerformdata.TaskId = imageviewer_crtask.TaskId
and imageviewer_crtask.RevProtId in (1));

然后你可以删除

DELETE FROM imageviewer_crreviewerformdata WHERE FormDataId IN (SELECT FormDataId FROM temp.tempdelete) 

然后删除临时表。

DROP TABLE temp.temptable.

这要求您有一个名为 temp 的文件夹。它也是一个很好的解决方案,可以在存储过程中更轻松地使用。

于 2013-10-22T10:58:23.617 回答