背景
有500条数据需要插入db,这500条数据如下
type | provider | |
---|---|---|
tzjsmf1@qq.com | 0 | tencent |
tzjsmf2@qq.com | 0 | tencent |
tzjsmf3@qq.com | 0 | tencent |
… | 0 | tencent |
tzjsmf500@qq.com | 0 | tencent |
可以看到只有mail字段有区别,且数字为依次递增,所以可以使用mysql循环语句进行插入。
解决方案
delimiter // #定义标识符为双斜杠
drop procedure if exists repeatInsert; #只允许存在一个repeatInsert的循环过程
create procedure repeatInsert() #创建循环过程
begin
declare i int; #申明变量
set i = 1; #变量赋值
repeat
INSERT INTO `zoom_pro_account` (`mail`, `type`, `provider`) VALUES (concat('tzjsmf', i, '@trip.com'), 0, 'zoom'); #使用concat进行拼接
set i = i + 1; #循环一次,i加一
until i > 500 end repeat; #结束循环的条件
end
// #结束定义语句
call repeatInsert();