【问题标题】:How would I limit the number of table rows?我将如何限制表格行数?
【发布时间】:2012-11-29 02:17:57
【问题描述】:

我有一个包含列的简单表格:

date - DATETIME
name - varchar(50)
text - varchar(200)

如何在添加第 11 行之前获取包含要删除的最早日期值的行,因此行数始终为 10?

【问题讨论】:

    标签: mysql sql database triggers


    【解决方案1】:

    使用触发器

    create TRIGGER trigger_name before insert on table_name FOR EACH ROW BEGIN if count > 10 then delete from table_name insert into table_name values(id, name , quantity); END if; END ;

    你也可以参考这个链接 http://dev.mysql.com/doc/refman/5.0/en/triggers.html

    【讨论】:

      【解决方案2】:

      使用删除查询创建删除后触发器:

           //get the row count
           IF rowCount > 10
             DELETE from myTable
             WHERE date = 
              (select date from 
                (select date from myTable
                order by date asc LIMIT 1) as tempTable
              )
            END IF;
      

      我建议这样做是因为删除查询中的相同表条件不起作用。 Here 是我的另一个答案。

      【讨论】:

        【解决方案3】:

        更新最旧的行而不是执行insert+delete?当然,您需要确保表中有 10 行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-23
          • 1970-01-01
          • 2014-01-31
          • 2011-11-16
          • 2022-01-15
          • 1970-01-01
          相关资源
          最近更新 更多