Search code examples
mysqlsqleventscopysql-insert

Scheduled event to copy data from one table to another in MySQL returns empty results


Running on phpMyAdmin local server: I have two tables: user (with data) and user_t (empty). They have identical column name, order and type (the user_t table has one extra column for timestamping the input though).

I want to create a scheduled event to check if the timestamp of each user's log has a certain value and when it does, I want to copy it to user_t while deleting it from user. Pretty straightforward.

The deletion works fine. It is the INSERT INTO user_t SELECT * FROM user WHERE user.reg < CURRENT_TIMESTAMP - INTERVAL 3 MINUTE; that gives me trouble.

This is the code I am using to create the event:

CREATE EVENT users_u
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO 
INSERT INTO user_t SELECT * FROM user WHERE user.reg < CURRENT_TIMESTAMP - INTERVAL 3 MINUTE;
DELETE FROM user WHERE reg < CURRENT_TIMESTAMP - INTERVAL 3 MINUTE;

However, it keeps returning empty result. So, I take it there must be something I am doing wrong. Could you find it in your hearts to help me make this code work? After all, it is from a tutorial and should be fine.

I have tried playing with the code and even considered looking into transaction but that seemed too bulky for this kind of operation.

EDIT

So after some playing with the code from nbk, I finally figured it out and here is the code that worked for me (I generalised the table and column names so they would not mislead anyone):

    DELIMITER $$
CREATE EVENT event_name 
ON SCHEDULE EVERY 1 MINUTE 
STARTS CURRENT_TIMESTAMP 
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR 
DO 
    BEGIN 
    INSERT INTO empty_table (col_name1, col_name2, col_name3, col_name4, col_name5) 
    SELECT col_name1, col_name2, col_name3, col_name4, col_name5 FROM data_table WHERE col_name < CURRENT_TIMESTAMP - INTERVAL 3 MINUTE; 
    DELETE FROM data_table WHERE col_name < CURRENT_TIMESTAMP - INTERVAL 3 MINUTE; 
    END $$
DELIMITER ;

Hopefully it helps someone like me in the future. Thanks again, nbk, for your quick help. Much appreciated and, of course, your answer is CORRECT.


Solution

  • You run every minute the event and want to enter the last three minutes, i dount that you have that many new inserts, or you need a python script or so that inserts for one hour ==> for testing purposes, I set up the frequency to 1 minute...

    Still you should rewrite your event.

    first do you really want duplicates, maybe a INSERT IGNORE,which would enter new row, if it violates a constraint

    second if you have mire than one statement you need a BEGIN END

    DELIMITER $$
    CREATE EVENT users_u
    ON SCHEDULE EVERY 1 MINUTE
    STARTS CURRENT_TIMESTAMP
    ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
    DO 
        BEGIN
            INSERT IGNORE INTO user_t SELECT * FROM user WHERE user.reg < CURRENT_TIMESTAMP - INTERVAL 3 MINUTE;
            DELETE FROM user WHERE reg < CURRENT_TIMESTAMP - INTERVAL 3 MINUTE;
        END $$
    DELIMITER ;