带有子查询的MYSQL DELETE给出错误代码:1235

时间:2018-08-14 09:26:10

标签: mysql mysql-workbench

我有一个表Job_Detail_History,其中包含以下结构和数据:

enter image description here

当我尝试使用带有LIMITNOT IN

的子查询删除查询时
DELETE FROM job_detail_history where id not in(select id from job_detail_history order by start_time desc limit 2);


是给我的:

  

错误代码:1235。此版本的MySQL尚不支持'LIMIT&   IN / ALL / ANY / SOME子查询”

然后我尝试通过SO找到一些解决方案:
我发现this one是全局解决方案,它告诉使用JOIN而不是NOT IN,但是在这种情况下,他们正在使用选择查询。因此,查询看起来像这样:

SELECT * from job_detail_history INNER JOIN (SELECT ID FROM job_detail_history order by start_time desc limit 2) as v2 limit 2;

,它将产生一个新表,如下所示:

enter image description here

所以,我的问题是在这种情况下如何处理DELETE情况?

1 个答案:

答案 0 :(得分:1)

通过使用联接,您也可以删除 下面是一个例子

DELETE t1,t2 FROM t1
        INNER JOIN
    t2 ON t2.ref = t1.id 
WHERE
    t1.id = 1;

根据您的情况稍作改动

SET SQL_SAFE_UPDATES = 0;  

    DELETE FROM job_detail_history where id not in
    ( select * from 
               (select id from 
                job_detail_history order by start_time desc limit 2)  
                as t1
   );