原来想搞个spring定时任务,那是很简单的事情,但是因为ShardingSphere不支持having,就没有办法,只好写个sql来实现,也不难。
CREATE DEFINER=`root`@`localhost` PROCEDURE `clean_pre_id`(in cn int)
BEGINDECLARE 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设置为trueDECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;-- 打开游标open id_cursor;-- 执行循环read_loop : LOOPFETCH id_cursor into v_as_id;IF done THENLEAVE read_loop;end if;-- 获取最大的idselect 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中定时任务的操作方法