Autorunning Query in mysql

大城市里の小女人 提交于 2019-11-27 11:22:20

问题


Is it possible to make a stored procedure that run every night 11 pm , check in table if any record is modified for last six month, If some record is modified for last six month I have to delete it from table. This has to run automatically without use of any external language.


回答1:


CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
COMMENT 'Clean up Service Start at 11:00PM daily!'
DO DELETE FROM my_table WHERE created_date < (NOW() - INTERVAL 1 MONTH);

OR for Stored Procedure.

CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
DO CALL my_sp_cleanup_old_data();



回答2:


you can achieve with mysql event scheduler--

http://dev.mysql.com/doc/refman/5.1/en/events.html

detail blog: http://goo.gl/6Hzjvg




回答3:


Create an event like below

CREATE EVENT e_daily
  ON SCHEDULE
    EVERY 1 DAY
 DO
   BEGIN
     DELETE FROM tableA WHERE DATE(`yourtimestamp`) <(CURDATE() - INTERVAL 6 MONTHS);
  END 



回答4:


You can also write a script that processes your data in Python, Perl, PHP, etc.

After that, simply setup cron entry using crontab -e and add following line:

0 23 * * * /path/to/my/script.pl 2>&1 >/dev/null

If you don't specify 2>&1 >/dev/null, you will get email with execution results.



来源:https://stackoverflow.com/questions/13872598/autorunning-query-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!