MySQL events with TRUNCATE and INSERT

MySQL

Can I create event that would first TRUNCATE table and then make INSERT?
I know how to TRUNCATE and INSERT in separate events but need to do it immediately one after the other. I just don't know the syntax for events.
I can do it in myPHPadmin

TRUNCATE tbl1;

INSERT INTO
tbl1
SELECT
....

I know I could you PHP to run those two queries. I prefer event.
Is it possible to do it in single event or at least to call those two event in other event.
BTW, are MySQL events ok to use or PHP file with cron would be better?
Thank you

Best Answer

You can create:

  1. Step#1 - stored procedure, which will run as many queries as You need - http://dev.mysql.com/doc/refman/5.7/en/create-procedure.html
  2. Step#2 - Native MySQL Events - which will call this stored procedure - https://dev.mysql.com/doc/refman/5.7/en/create-event.html

In case when You do not want use MySQL bud scheduler, You always can call this stored procedure from PHP, cron, any other SQL script and etc