mysql 用当前时间当表名来复制另一张表的表结构,并复制原表30天之前的数据,然后删除原表30天前的数据
SET @sqlstr = CONCAT('create table tpl_cm_msg_old_',DATE_FORMAT(CURDATE(),'%Y%m%d'),' like tpl_cm_msg');
PREPARE stmt1 FROM @sqlstr ;
EXECUTE stmt1 ;
SET @sqlstr = CONCAT('insert into tpl_cm_msg_',DATE_FORMAT(CURDATE(),'%Y%m%d'),' SELECT * FROM tpl_cm_msg
WHERE id <= (SELECT MAX(id) FROM tpl_cm_msg WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) >= DATE(created_dt))');
PREPARE stmt1 FROM @sqlstr ;
EXECUTE stmt1 ;
delete from tpl_cm_msg where id <= (SELECT max(id) FROM tpl_cm_msg
WHERE id <= (SELECT MAX(id) FROM tpl_cm_msg WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) >= DATE(created_dt)))