需求:
删除tb1数据但保留最近5条记录:版本:
mysql 5.0.83表tb1:
create table tb1
(
intime datetime,
txt varchar(10)
);

解决方案 »

  1.   

    delete a from tb1 a left join (select intime from tb1 order by intime desc limit 5) b on a.intime=b.intime
    where b.intime is null
      

  2.   

    假设intime唯一delete a from tb1 a left join (select intime from tb1 order by intime desc limit 5) b on a.intime=b.intime
    where b.intime is null
      

  3.   


    原来还有delete a from tb1 a的用法啊,我还准备做一个临时表,把5条记录写入数据库,TRUNCATE TABLE tb1后,
    再把临时表记录写入tb1;问下狼头大大,我这种做法是不是在tb1有大量数据量时,效率更高?
      

  4.   

    是的,宁可TRUNCATE然后再加5条。 TRUNCATE基本上不需要时间,加五条记录也很多,但如果是删除1000条的话,显然不如你的这个方法快。
      

  5.   


    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html