dowbwrr3590709 2013-01-18 00:39
浏览 63
已采纳

设置MySQL表中每个userid的最大条目数

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? I want to make it so after a user types say 5 updates, any more updates entered after that deletes the oldest update. I know how to do this through PHP, just curious if there's any way to do this through MySQL only.

  • 写回答

2条回答 默认 最新

  • duanbishai5271 2013-01-18 00:50
    关注

    The only way I can think of doing this database-side would be to use a TRIGGER on the table. 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

    Good luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?