【问题标题】:Delete rows until a defined number?删除行直到定义的数字?
【发布时间】:2014-03-09 23:21:57
【问题描述】:

我的服务器给了我 10000 行的限制,我的想法是我删除给定表中的所有旧行并留下一定数量的行。 示例:我在“posts”表中有 10,000 行,我希望命令删除所有最旧的我离开,直到 5000。

更多信息:

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

【问题讨论】:

    标签: postgresql numbers delete-row


    【解决方案1】:
    delete from posts where pubdate <= 
      (select pubdate from posts order by pubdate desc limit 1 offset 5000)
    

    【讨论】:

    • 这假设 pubdate 决定什么是最旧和最新的
    • 谢谢!这项工作:DELETE FROM posts WHERE pubdate
    • 很高兴它有帮助! :) 您很可能希望在 pubdate 上有一个索引
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2014-11-17
    相关资源
    最近更新 更多