Question

My server gives me a limit of 10000 rows, and the idea is that I delete all the old rows in a given table and me to leave a certain number of rows. Example: I have 10,000 rows in the table "posts" and I want the command to delete all the oldest me leave until 5000.

More info:

create_table "posts", :force => true do |t|
 t.string   "title"
 t.string   "slug"
 t.string   "link"
 t.datetime "pubdate"
 t.text     "description"
 t.integer  "blog_id"
end
Was it helpful?

Solution

delete from posts where pubdate <= 
  (select pubdate from posts order by pubdate desc limit 1 offset 5000)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top