mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法
#分开两个sql执行正常的语句,只保留最新1000条数据,删掉1000条以前的旧数据
select number from historydata order by number desc limit 1001,1;
delete from historydata where number < 201911270538;
#直接合并后报错:错误代码: 1093 You can't specify target table 'historydata' for update in FROM clause
delete from historydata where number < (select number from historydata order by number desc limit 1001,1);
#意思是:在同一语句中,不能先select出同一表中的某些值,再update这个表
#将select出的结果再通过中间表select一遍,可以规避这个错误
delete from historydata where number < (
select w.number from (
select historydata.number from historydata order by historydata.number desc limit 1001,1
)w
);
注意:select w.number 这个w前面不能有多个空格或者tab键的隐藏字符,否则会报找不到 w.number的错误。