Mysql regularly draws from one table a to another table b

-- 1. Create tables a and b first

CREATE TABLE `a` (
  `zyid` int(11) NOT NULL COMMENT 'Resource ID',
  `hdbs` varchar(32) COLLATE utf8_bin NOT NULL COMMENT 'Activity ID',
  `hdqy` char(6) COLLATE utf8_bin DEFAULT NULL COMMENT 'Activity area',
  `hdsj` timestamp NOT NULL COMMENT 'Activity time',
  `rksj` varchar NULL DEFAULT NULL COMMENT 'Storage time'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='test table a';

CREATE TABLE `b` (
  `zyid` int(11) NOT NULL COMMENT 'Resource ID',
  `hdbs` varchar(32) COLLATE utf8_bin NOT NULL COMMENT 'Activity ID',
  `hdqy` char(6) COLLATE utf8_bin DEFAULT NULL COMMENT 'Activity area',
  `hdsj` timestamp NOT NULL COMMENT 'Activity time',
  `rksj` varchar NULL DEFAULT NULL COMMENT 'Storage time'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='test table b';

-- 2. Create a stored procedure If there is a problem with select * insertion, list all the fields to convert the time
create DEFINER=`root`@`%` PROCEDURE `pro_rpc`( )
BEGIN
 DECLARE max_rksj VARCHAR(128);
 select max(rksj ) into max_rksj from b;
 if max_rksj is null then set max_rksj='2011-04-13 13:32:24';
 end if;
  insert into b
select * from at where rksj>max_rksj;
commit;
END

Note: If there is an error in creating the stored procedure, it may be a problem with the delimiter . The default delimiter of mysql is ";", while "from table_name" is followed by ";". Mysql defaults to the end of the sql statement, so there is an error. The solution is to change the delimiter:delimiter //

-- 3. Create a timed task, and draw it every ten minutes

CREATE DEFINER=`root`@`%` EVENT `pro_rpc_inc` ON SCHEDULE EVERY 10 MINUTE STARTS '2022-04-22 13:20:21' ON COMPLETION NOT PRESERVE ENABLE DO call pro_rpc()

Note: Remember to check whether the scheduled task of the database is enabled, show variables like '%event%'
if event_scheduler is OFF, execute set global event_scheduler=ON

 

Guess you like

Origin blog.csdn.net/chunzhi128/article/details/124355470