简体   繁体   中英

Scheduled event in mysql

I have two tables.. players which has details of all the players and players_copy which is empty.

I need to write a scheduled event script in sql wherein after 1 minute, 10 rows from players get inserted into the players_copy. and after 2 minutes, 20 rows gets inserted..after 3 minutes 30 rows and so on..I have written the following script

CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 5 MINUTE
ON COMPLETION PRESERVE
DO
   insert into players_copy
 (
  Player_ID,
  Attributes,
 imagePath, 
 Full_Name, 
 Short_Name, 
 Team_Full_Name,
  Team_Short_Name,
  Team_ID,
  Commentry_Name
 ) 
SELECT *
  FROM players
  ORDER BY Team_Full_Name
 limit 10

But its executing the same 10 rows over and over again..its not incrementing..can someone help with logic as to how can i update the data such that i achieve the above mentioned condition and should i be using Stored procedures or triggers along with this.

Try something like:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.17    |
+-----------+
1 row in set (0.00 sec)

mysql> SET GLOBAL event_scheduler = ON;
Query OK, 0 rows affected (0.00 sec)

mysql> USE `_`;
Database changed

mysql> DROP TABLE IF EXISTS `players`, `players_copy`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `players` (
    ->   `player_id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    ->   `full_name` VARCHAR(255)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `players_copy` (
    ->   `player_id` BIGINT UNSIGNED,
    ->   `full_name` VARCHAR(255),
    ->   `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `players` (`full_name`) VALUES
    -> ('Player 1'), ('Player 2'), ('Player 3'), ('Player 4'),
    -> ('Player 5'), ('Player 6'), ('Player 7'), ('Player 8'),
    -> ('Player 9'), ('Player 10'), ('Player 11'), ('Player 12'),
    -> ('Player 13'), ('Player 14'), ('Player 15'), ('Player 16'),
    -> ('Player 17'), ('Player 18'), ('Player 19'), ('Player 20'),
    -> ('Player 21'), ('Player 22'), ('Player 23'), ('Player 24'),
    -> ('Player 25'), ('Player 26'), ('Player 27'), ('Player 28'),
    -> ('Player 29'), ('Player 30'), ('Player 31'), ('Player 32'),
    -> ('Player 33'), ('Player 34'), ('Player 35'), ('Player 36'),
    -> ('Player 37'), ('Player 38'), ('Player 39'), ('Player 40'),
    -> ('Player 41'), ('Player 42'), ('Player 43'), ('Player 44'),
    -> ('Player 45'), ('Player 46'), ('Player 47'), ('Player 48'),
    -> ('Player 49'), ('Player 50'), ('Player 51'), ('Player 52');
Query OK, 52 rows affected (0.00 sec)
Records: 52  Duplicates: 0  Warnings: 0

mysql> DELIMITER //

mysql> DROP EVENT IF EXISTS `test_event_03`//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE EVENT `test_event_03` ON SCHEDULE EVERY 1 MINUTE
    -> STARTS CURRENT_TIMESTAMP
    -> ENDS CURRENT_TIMESTAMP + INTERVAL 5 MINUTE
    -> ON COMPLETION PRESERVE
    -> DO
    -> BEGIN
    ->   DECLARE `_starts` DATETIME DEFAULT
    ->     (SELECT `STARTS`
    ->      FROM `information_schema`.`EVENTS`
    ->      WHERE `EVENT_SCHEMA` = '_' AND `EVENT_NAME` = 'test_event_03');
    ->   DECLARE `_range` TINYINT DEFAULT
    ->     (SELECT (TIMESTAMPDIFF(SECOND, `_starts`, CURRENT_TIMESTAMP()) / 60) * 10);
    ->   INSERT INTO `players_copy`
    ->   SELECT `player_id`, `full_name`
    ->   FROM `players`
    ->   LIMIT `_range`;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM