-- 创建存储过程 CREATE DEFINER=`root`@`localhost` PROCEDURE `rename_file_name`() begin DECLARE flag INT; DECLARE errfId BIGINT(20); -- e_relative_record_file id DECLARE efrId BIGINT(20); -- e_file_record id DECLARE renameFileName VARCHAR(200); -- 重新生成的图片文件名称 DECLARE target CURSOR FOR SELECT errf.id errfId,efr.id efrId,concat(substring_index(errf.file_name, '_', 1),'-',FLOOR(100+( RAND()*900)),'_cut0.jpg') renameFileName FROM e_relative_record_file errf INNER JOIN e_file_record efr ON errf.file_name = efr.file_name GROUP BY errf.file_name HAVING COUNT(errf.file_name)>1; -- 游标 查询重复的文件名称的数据 SET flag=0; OPEN target; REPEAT FETCH target into errfId,efrId,renameFileName; -- 变量赋值 UPDATE e_relative_record_file SET file_name = renameFileName WHERE id= errfId; -- 更新e_relative_record_file表重复的图片名称 UPDATE e_file_record SET file_name = renameFileName WHERE id= efrId; -- 更新e_file_record表重复的图片名称 UNTIL flag END REPEAT; CLOSE target; end -- 运行存储过程 call rename_file_name(); -- 删除存储过程 DROP PROCEDURE rename_file_name;