简体   繁体   English

设置MySQL表中每个用户ID的最大条目数

[英]setting max number of entries per userid in MySQL table

Say I have the following table 说我有下表

//table user_update
update_id   |   userid   |   update_message   |   timestamp

Is there a way to set a maximum number of entries per userid? 有没有一种方法可以设置每个用户ID的最大条目数? I want to make it so after a user types say 5 updates, any more updates entered after that deletes the oldest update. 我要这样做,以便在用户键入说5个更新后,在此之后输入的任何其他更新将删除最早的更新。 I know how to do this through PHP, just curious if there's any way to do this through MySQL only. 我知道如何通过PHP做到这一点,只是好奇是否有办法仅通过MySQL做到这一点。

That is actually possible. 这实际上是可能的。 but it is questionable if you really want to make that effort. 但是,如果您真的想做出这样的努力,那就值得怀疑。

Read a little about 'triggers'. 阅读一些有关“触发器”的信息。 You can use a trigger to start an action when certain conditions are met. 当满足某些条件时,可以使用触发器来启动操作。 This way you can trigger a delete action on every insert action on that table. 这样,您可以对该表上的每个insert操作触发delete操作。 Then all that is left is a condition that up to five entries are kept. 那么剩下的就是最多保留五个条目的条件。

The only way I can think of doing this database-side would be to use a TRIGGER on the table. 我可以想到的在数据库侧进行操作的唯一方法是在表上使用TRIGGER Maybe something like this: 也许是这样的:

CREATE TRIGGER check_for_too_many_rows
AFTER INSERT ON User_Update
FOR EACH ROW BEGIN
   DO SOME LOGIC TO CHECK THE COUNT AND DELETE IF MORE THAN 5...
END;

Here is some additional information: 以下是一些其他信息:

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

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM