SQLite 数据库中只保留最后 N 条记录,按日期排序 [英] Keep only N last records in SQLite database, sorted by date

查看:20
本文介绍了SQLite 数据库中只保留最后 N 条记录,按日期排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SQLite 数据库,我需要执行以下操作:仅保留最后 N 条记录,按日期排序.你是怎么做到的?

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?

推荐答案

删除除最新 10 条记录之外的所有记录.

To delete all but the latest 10 records.

delete
from test
where id not in (
    select id
    from test
    order by date desc
    limit 10
)

这篇关于SQLite 数据库中只保留最后 N 条记录,按日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆