Alex Rivera | Logout

Mysql delete order by

Asked 2012-09-12T06:21:20.320
22

I have a table and I only display the latest 30 rows by order by ID.

I'm trying to delete any rows after the 30 newest rows by using this query below.

DELETE FROM table WHERE type = 'test' ORDER BY id DESC LIMIT 30, 60

I keep getting this error below

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 60' at line 1

What am I doing wrong?

Edit
Report

1 Answer

8

Second edit: While MySQL supports LIMIT in delete statements, it does not allow an OFFSET. This means that you cannot skip the first 30 rows.

Make a subselect on id (or any other primary key):

DELETE FROM table WHERE id IN (SELECT id FROM table WHERE type = 'test' ORDER BY id DESC LIMIT 30, 60)
answered 2012-09-12T06:23:33.550

Your Answer