Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite how delete last added entry of a table

I'm trying to delete the last added entry of a table:

DELETE FROM notes ORDER BY created_at DESC LIMIT 1

This just causes the following error:

near "ORDER": syntax error

Why might I be getting this error? (notes exists and has records in it!)

like image 579
Markus Avatar asked Nov 03 '10 13:11

Markus


People also ask

How can you delete the existing records from a table in SQLite?

SQLite DELETE Query is used to delete the existing records from a table. You can use WHERE clause with DELETE query to delete the selected rows, otherwise all the records would be deleted.

How do I delete multiple records in SQLite?

If you wanted to delete a number of rows within a range, you can use the AND operator with the BETWEEN operator. DELETE FROM table_name WHERE column_name BETWEEN value 1 AND value 2; Another way to delete multiple rows is to use the IN operator.

How do you VACUUM SQLite?

The VACUUM command works by copying the contents of the database into a temporary database file and then overwriting the original with the contents of the temporary file. When overwriting the original, a rollback journal or write-ahead log WAL file is used just as it would be for any other database transaction.

How can you update the existing records from a table in SQLite?

First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.


2 Answers

Try this

DELETE FROM notes WHERE id = (SELECT MAX(id) FROM notes);
like image 186
srinathhs Avatar answered Sep 20 '22 13:09

srinathhs


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

Watch out, this will not limit the number of rows deleted. If there are more than one row at max(created_at), this will delete all of them because the subject you specified does not exist (last added entry of a table).

like image 24
0xCAFEBABE Avatar answered Sep 19 '22 13:09

0xCAFEBABE