【问题标题】:How to schedule events and create triggers with mysql如何使用 mysql 安排事件和创建触发器
【发布时间】:2016-10-20 13:16:44
【问题描述】:

我的数据库中有 3 个表,tbl_events、tbl_exceptions、tbl_archived_events

tbl_events 存储事件列表,其中包含以下字段

**eventID** => int(4)Key, AutoIncrement
eventREF => VARCHAR (4) ( will remain the same for each version of the event and will be used to check for an exception)  
eventName => VARCHAR (30) (Name of the event)
eventType => int(4) will determine the type of event it is
eventLocation => VARCHAR (30) hold the event location data
eventDate => DATETIME hold the date of the event
eventStart => DATETIME hold the start time of the event
eventEnd => DATETIME hold the end time of the event
isReoccuring => int(2) Default to 1 which means it is reoccurring 
frequency => VARCHAR (10) will be either Daily/Weekly/Monthly/Yearly
eventLink => VARCHAR (30) will contain a link to the event page if there is one
eventValid => int(2) Will be set to 1 if the event is on and 0 if there is an exception

tbl_exceptions 存储一组事件及其不举行的日期。有时可能不会举行重复性事件。该表将保存以下字段信息

**exceptionID** KeyField, AutoIncrement => int(4)
eventREF => VARCHAR (4) Holds the event ref number
exceptionDate => DATETIME , Hold the date of the exception

tbl_archive_events 将存储过去已过期的事件。此表将存储与 tbl_events 表相同的数据,但仅用于过去的事件

**eventID** => int(4)Key, AutoIncrement
eventREF => VARCHAR (4) ( will remain the same for each version of the event and will be used to check for an exception)  
eventName => VARCHAR (30) (Name of the event)
eventType => int(4) will determine the type of event it is
eventLocation => VARCHAR (30) hold the event location data
eventDate => DATETIME hold the date of the event
eventStart => DATETIME hold the start time of the event
eventEnd => DATETIME hold the end time of the event
isReoccuring => int(2) Default to 1 which means it is reoccurring 
frequency => VARCHAR (10) will be either Daily/Weekly/Monthly/Yearly
eventLink => VARCHAR (30) will contain a link to the event page if there is one
eventValid => int(2) Will be set to 1 if the event is on and 0 if there is an exception[/CODE]

所以一旦一个事件已经过期,我希望 mysql 执行以下操作:

  • 1/ 如果事件是重复发生的事件,则创建一个新事件并设置 eventDate 日期相对于在 经常与现有事件相关的字段。
  • 2/ 如果新事件 在 eventException 表中找到 eventREF 然后设置 eventValid 到 0
  • 3/ 一旦事件过期,将事件复制到 eventsArchive 表
  • 4/ 从 tbl_events 表中删除过期事件

到目前为止我有这个

DELIMITER $$
CREATE 
EVENT `new_event` 
ON SCHEDULE EVERY 1 DAY STARTS '2016-07-24 03:00:00' 
DO BEGIN

       -- create new event
       SELECT eventID, eventREF, eventTitle, eventLocation, eventDate, eventStart, eventEnd, isReoccuring, frequency, eventType, eventLink, eventValid
       FROM tbl_events
       WHERE eventDate < now()

       --- for each event found create a new event
       INSERT INTO tbl_events (eventID, eventREF, eventTitle, eventLocation, eventDate, eventStart, eventEnd, isReoccuring, frequency, eventType, eventLink, eventValid) 


       -- copy expired events to tbl_archived_events
       INSERT INTO tbh_archived_events (eventID, eventREF, eventTitle, eventLocation, eventDate, eventStart, eventEnd, isReoccuring, frequency, eventType, eventLink, eventValid) 
       SELECT eventID, eventREF, eventTitle, eventLocation, eventDate, eventStart, eventEnd, isReoccuring, frequency, eventType, eventLink, eventValid
       FROM tbl_events
       WHERE eventDate < now();

       -- delete expired events from tbl_events
       DELETE FROM tbl_events WHERE eventDate < now();

END */$$[/PHP]

显然以上是不正确的,我不确定我在做什么,希望能得到一些帮助 谢谢

卢克

【问题讨论】:

    标签: php sql mysql-event


    【解决方案1】:

    您可以使用 mysql 调度程序每 5 秒或每 1 天运行一次。

    http://dev.mysql.com/doc/refman/5.1/en/create-event.html

    没有人用这个东西


    创建活动您的活动
    每隔 1 天(第二天)安排一次

    调用你的STROREPROCEDURE();


    单独创建您的 SP

    【讨论】:

    • 感谢您的意见,我会查看链接,看看我是否可以得到一些工作。
    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 2011-02-03
    • 2016-09-19
    • 2016-10-20
    相关资源
    最近更新 更多