2013-04-01 68 views
0

我想在每月初在一個表中插入數據,我雖然用mysql事件。但有人告訴我,如果我錯過了這個事件(意思是如果我的服務器在預定的日期/時間內停止,那麼mysql在上線後不會重新運行該事件,不確定),那麼mysql不會再重新運行它。MySql的時間表事件

所以我創建了一個表開孔的event_balance這裏事件將被記錄。然後,我創建了一個名爲balance_update_check事件將每小時運行一次,並檢查事件本月執行,如果不是那麼它會調用這將爲本月創建行,然後更新表event_balanceevent_typedate的過程。

但我並不滿足於此,請檢查我的代碼,並告訴我,好嗎?如有可能提供更好的解決方案。謝謝你的時間。

事件balance_update_check

CREATE 
    EVENT `balance_update_check` 
    ON SCHEDULE EVERY 1 Hour 
    DO BEGIN 
     set @check_if_cont_doc:= (SELECT count(*) FROM event_balance WHERE event_type = 'doctor_contract' and monthname(event_balance.date) = monthname(CURDATE()));   

     set @check_if_cont_other:= (SELECT count(*) FROM event_balance WHERE event_type = 'other_contract' and monthname(event_balance.date) = monthname(CURDATE())); 

     IF (@check_if_cont_doc = 0) Then 
      call proc_leave_bal_doc_cont(); 
      Insert into event_balance (event_type,`date`) values('doctor_contract',CURDATE())  
     END IF;  

     IF (@check_if_cont_other = 0) Then 
      call proc_leave_bal_other_cont(); 
      Insert into event_balance (event_type,`date`) values('other_contract',CURDATE())   
     END IF; 
END // 
+0

我有一個解決方案在這裏,但並不真正相關。您是否在使用每月會費的系統? –

+0

不,但每月計劃的事件的任何例子都會幫助我 – user007

回答

1

你要確保該event_scheduler設置爲ON在my.cnf文件。轉到my.cnf文件(如果您使用Windows,則使用my.ini文件),並註釋掉event_scheduler=DISABLED或設置event_scheduler=ON。然後重啓。這是確保您的事件在服務器重新啓動後運行。有一個關於它在這裏的一些詳細信息:dev.mysql.com/doc/refman/5.5/en/events-configuration.html