17

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!)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Markus
  • 3,948
  • 8
  • 48
  • 64

2 Answers2

26

Try this

DELETE FROM notes WHERE id = (SELECT MAX(id) FROM notes);
hichris123
  • 10,145
  • 15
  • 56
  • 70
srinathhs
  • 1,998
  • 4
  • 19
  • 33
2
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).

0xCAFEBABE
  • 5,576
  • 5
  • 34
  • 59
  • Thats the solution for me! Thanks for your hint! I just use the max(id) to make sure, that there is really just the last entered id... – Markus Nov 03 '10 at 13:25