Wednesday, July 5, 2017

MySQL LIMIT on DELETE statement

Suppose you want to delete some old data from a table but need to keep latest data to light weight your database. In that case there are some very simple way to do.

Here one of them. If there is id unique primary column with auto increment, in that table we can delete old id like if you want to delete first 10000 rows  with following command.

"DELETE FROM table_name WHERE id >=10000";
*The conditions in the optional WHERE clause identify which rows to delete. Without WHERE clause, all rows will be deleted.

If the DELETE statement includes an ORDER BY clause, rows are deleted in the order specified by the clause. This is useful primarily in conjunction with LIMIT. For example, the following statement finds rows matching the WHERE clause, sorts them by timestamp_column, and deletes the first (oldest) one:
DELETE FROM somelog WHERE user = 'jcole'
ORDER BY timestamp_column LIMIT 1;
But it's not possible to limit rang like SELECT statement LIMIT 1,500

No comments:

Post a Comment

Total Pageviews