sqlite3:删除 id='foo' 之外最新的 n 条记录的单个语句

2024-10-02 12:29:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力在Python中实现sqlite。这就是让我难堪的原因。你知道吗

给定以下数据结构:

id archiveSeconds archiveSize
-- -------------- -----------
13 1234567        100
34 1234568        100
13 1234568        100
24 1234570        100

。。。你知道吗

我想基于archiveSeconds删除除最近n条记录外的所有特定ID的记录,并保留所有其他记录不变。你知道吗

我能想到的最接近的事情是:

delete from Archives where id='13' not in (select id from Archives order by archiveSeconds desc limit 15);

但这会清空表中的所有记录。我想我需要嵌套几个select语句来实现这一点,但是我对完成这一点的sql语句感到迷茫。你知道吗


Tags: fromid数据结构sqlite记录原因语句where
1条回答
网友
1楼 · 发布于 2024-10-02 12:29:31

两个变化。首先,您不希望比较子查询中的id,而是希望比较时间。其次,要筛选给定id的子查询:

delete from Archive
     where id='13' and
           ArchiveSeconds not in (select ArchiveSeconds
                                  from Archives
                                  where id = '13'
                                  order by archiveSeconds desc
                                  limit 15);

我注意到一个表叫做Archive,另一个叫做Archives。这是您最初的查询。你知道吗

相关问题 更多 >

    热门问题