MySQL 表作为 FIFO/队列

2022-01-21 00:00:00 queue stack fifo mysql

我们如何将 Mysql 表视为有限的 FIFO 缓冲区(队列).

How can we treat a Mysql table as a limited FIFO buffer (Queue).

目标是:

  1. 一次表格只能有 N 行.
  2. 当插入一行时,应该删除最旧的行以保持行数为 N.

请建议方法.

更新:抱歉,正如许多人指出的那样,我将问题从 STACK 更改为 FIFO 队列

UPDATE: Sorry guys, as many pointed I changed my question from STACK to FIFO queue

推荐答案

过去的 Mysql 5 你可以使用触发器来实现这一点.

Past Mysql 5 you could use a trigger to achieve this.

http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

那么你触发的 sql 将沿着这条线:

then your triggered sql would be along the lines off:

DELETE FROM foo WHERE id NOT IN (SELECT id FROM foo ORDER BY id DESC LIMIT 10)

相关文章