从数据库中删除除前两行之外的所有选定行

时间:2012-10-31 09:16:56

标签: sql database sqlite

除了前两个选定日期,我需要删除数据库中的所有日期:

SELECT DateColumn 
FROM Table 
GROUP BY date(DateColumn) 
ORDER BY DateColumn DESC

我希望在一个查询中删除除前两个之外的所有日期。

2 个答案:

答案 0 :(得分:4)

delete from table where DateColumn not in(
SELECT DateColumn 
FROM Table 
GROUP BY date(DateColumn) 
ORDER BY DateColumn DESC LIMIT 2)

答案 1 :(得分:3)

DELETE FROM Table
      WHERE DateColumn NOT IN (SELECT DateColumn 
                                 FROM Table 
                             GROUP BY date(DateColumn) 
                             ORDER BY DateColumn DESC
                                LIMIT 2);