-1

I have a table with 2.500.000 rows. All this rows has a value of timestamp. I want to keep 100 rows that has the biggest timestamp.. All the other values i want to be deleted!!

$values_to_delete = db_delete('error_log_jira')
    ->condition('timestamp', 1423731524, '>')
    ->execute();

I have this code and works perfect but i do not know how to crate the thing that i want is someone here that can help me?? I need a real help!!

Karmen
  • 238
  • 2
  • 14
  • how to create the things that i want .. what do you mean by that – Ameya Deshpande Apr 15 '15 at 12:04
  • with the word things i mean the conditions that i told before – Karmen Apr 15 '15 at 12:05
  • 1
    Please consider following answer: http://stackoverflow.com/questions/7142097/mysql-delete-statement-with-limit – Andriy Apr 15 '15 at 12:10
  • This should really be marked as a duplicate of https://stackoverflow.com/questions/578867/sql-query-delete-all-records-from-the-table-except-latest-n. The duplicate question currently linked at the top has been deleted. – Ilmari Karonen Apr 08 '18 at 21:08

1 Answers1

0

Let's say your table which has 2.500.000 rows is called TableA. You can do this:

  1. Create a temporary table TableB which has the same columns as TableB

  2. Write a query which extracts 100 rows depending on the timestamp

  3. Insert these 100 rows in TableB

  4. Delete all the rows of TableA

  5. Insert all the rows of TableB in TableA and drop TableB

Steps 2-4 can be done in sql by a query like this:

insert into TableB (SELECT * FROM TableA ORDER BY timestamp DESC LIMIT 100)
Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
Sonia Saxena
  • 95
  • 2
  • 10