How to schedule a MySQL query?

you have 2 basic options (at least):

1, Take a look at Event Scheduler

First create table eg. stock_dumps with fields

itemcode, quantity, avgcost, ttlval,dump_date (DATETIME)

CREATE EVENT `Dumping_event` ON SCHEDULE
        EVERY 1 DAY
    ON COMPLETION NOT PRESERVE
    ENABLE
    COMMENT ''
    DO BEGIN
INSERT INTO stock_dumps(itemcode, quantity, avgcost, ttlval,dump_date)
SELECT itmcode, quantity, avgcost, (avgcost * quantity)as ttlval, NOW()
  FROM table_1 JOIN table_2 ON table_1.itmcode = table_2.itmcode;
END

Please follow instructions how to enable scheduler on link posted above.
Note : Old versions of mysql don’t have event scheduler

2, Create cron job/windows scheduled job:

create sql file:

INSERT INTO stock_dumps(itemcode, quantity, avgcost, ttlval,dump_date)
SELECT itmcode, quantity, avgcost, (avgcost * quantity)as ttlval, NOW()
FROM table_1 JOIN table_2 ON table_1.itmcode = table_2.itmcode;

schedule this command:

mysql -uusername -ppassword < /path/to/sql_file.sql

Leave a Comment