Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sqlite3:删除id='foo'_Python_Sql_Sqlite - Fatal编程技术网

Python sqlite3:删除id='foo'

Python sqlite3:删除id='foo',python,sql,sqlite,Python,Sql,Sqlite,我正在努力在Python内部实现sqlite。这就是让我感到困惑的原因 给定以下数据结构: id archiveSeconds archiveSize -- -------------- ----------- 13 1234567 100 34 1234568 100 13 1234568 100 24 1234570 100 我想根据archiveSeconds删除特定ID的所有记录(除了最近的n条记录),并保留所有其他记录不变 我

我正在努力在Python内部实现sqlite。这就是让我感到困惑的原因

给定以下数据结构:

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

我想根据archiveSeconds删除特定ID的所有记录(除了最近的n条记录),并保留所有其他记录不变

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

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

但这会清空表中的所有记录。我想我需要嵌套几个select语句来实现这一点,但我对sql语句的理解不够。有两个更改。首先,您不希望比较子查询中的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);

我注意到一个表称为归档,另一个表称为归档。这是您的原始查询。

不确定这是否与Python相关-也许sql更合适您是对的。我只是想给出一个完整的背景。时间可能在两行或更多行中相同,因此归档也可能相同。如果是的话,我想保留这两条记录,前提是它们都在最近15条记录中。如果我理解这个查询,它依赖于archiveSeconds作为比较。基于此,这难道不会删除看似重复的记录吗?太棒了!如广告所示,我非常感谢您的帮助。