mysql delete with subquery出现错误code:1235

pnwntuvh  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(310)

我有一张table Job_Detail_History ,包含以下结构和数据:

当我尝试使用子查询删除查询时 LIMIT , NOT 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子查询
然后我试着找到一些解决办法:
我发现这是一个全球性的解决方案,它告诉使用 JOIN 而不是 NOT IN ,但在这种情况下,它们使用的是select查询。因此,查询如下所示:

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

它将生成一个新表,结果如下:

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

xmq68pz9

xmq68pz91#

通过使用join,您也可以删除,下面是一个示例

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
   );

相关问题