Sqlite如何删除最后添加的表项

时间:2010-11-03 13:11:23

标签: sqlite sql-delete

我正在尝试删除表格的最后添加条目:

DELETE FROM notes ORDER BY created_at DESC LIMIT 1

这只会导致以下错误:

near "ORDER": syntax error

为什么我会收到此错误? (notes存在并且有记录!)

3 个答案:

答案 0 :(得分:15)

试试这个

DELETE FROM notes WHERE id = (SELECT MAX(id) FROM notes);

答案 1 :(得分:2)

delete from notes where created_at = ( select max(created_at) from notes );

注意,这不会限制删除的行数。如果max(created_at)上有多行,则会删除所有这些行,因为您指定的主题不存在(最后添加的表条目)。

答案 2 :(得分:0)

查看此帖子,它使用嵌套查询来实现您要执行的操作:

http://sqlblogcasts.com/blogs/simons/archive/2009/05/22/UPDATE-and-DELETE-TOP-and-ORDER-BY---Part2.aspx

相关问题