Keep only N last records in SQLite database, sorted by date

后端 未结 4 1308
渐次进展
渐次进展 2020-12-13 15:27

I have an SQLite database that I need to do the following: Keep only last N records, sorted by date. How do you do that?

4条回答
  •  一个人的身影
    2020-12-13 15:59

    to keep only the last 10 records, think inverted.

    To delete the older 10 records:

    DELETE FROM Table_name 
    WHERE date in (SELECT date FROM Table_name ORDER BY Date Desc Limit -1 
                   OFFSET  (select count(*)-10 from Table_name) );
    

    Let me know how it worked for you!

提交回复
热议问题