原来想搞个spring定时任务,那是很简单的事情,但是因为ShardingSphere
不支持having
,就没有办法,只好写个sql来实现,也不难。
CREATE DEFINER=`root`@`localhost` PROCEDURE `clean_pre_id`(in cn int)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE v_as_id int;
declare v_max_id int;
# 定义游标
DECLARE id_cursor CURSOR FOR select as_id from acc_id_table group by as_id having count(*)>cn;
-- 游标中的内容执行完后将done设置为true
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;
-- 打开游标
open id_cursor;
-- 执行循环
read_loop : LOOP
FETCH id_cursor into v_as_id;
IF done THEN
LEAVE read_loop;
end if;
-- 获取最大的id
select max(id) into v_max_id from acc_id_table where as_id=v_as_id and id_type='02';
-- 删除之前的数据
delete from acc_id_table where as_id=v_as_id and id_type='02' and id<v_max_id-cn;
--
END LOOP read_loop;
-- 释放游标
CLOSE id_cursor;
END
定时任务创建参见MySql中定时任务的操作方法