mysql数据库如何循环_mysql数据库如何循环语句

ce924761f3fad9401f1d450fe02ccd74.png

mysql数据库循环语句的方法:

循环编写方式1:while关键字delimiter //                            #定义标识符为双斜杠

drop procedure if exists whileTest;          #如果存在whileTest存储过程则删除

create procedure whileTest()                 #创建无参存储过程,名称为whileTest

begin

declare i int;                            #申明变量

set i = 0;                               #变量赋值

while i 

insert into maomao(keyId) values (i);    #往maomao表添加数据

set i = i + 1;                  #循环一次,i加一

end while;                          #结束while循环

select * from maomao;                 #查看maomao表数据

end

//                                      #结束定义语句

call whileTest();                            #调用存储过程

循环编写方式1:repeat关键字delimiter //                            #定义标识符为双斜杠

drop procedure if exists repeatTest;          #如果存在repeatTest存储过程则删除

create procedure repeatTest()                 #创建无参存储过程,名称为repeatTest

begin

declare i int;                      #申明变量

set i = 0;                          #变量赋值

repeat

insert into maomao values (i);    #往maomao表添加数据

set i = i + 1;                  #循环一次,i加一

until i > 80 end repeat;            #结束循环的条件: 当i大于80时跳出repeat循环

select * from maomao;                 #查看maomao表数据

end

//                                      #结束定义语句

call repeatTest();                            #调用存储过程

循环编写方式1:loop关键字delimiter //                            #定义标识符为双斜杠

drop procedure if exists loopTest;          #如果存在test存储过程则删除

create procedure loopTest()                 #创建无参存储过程,名称为loopTest

begin

declare i int;                      #申明变量

set i = 0;                          #变量赋值

lp : loop                           #lp为循环体名,可随意 loop为关键字

insert into maomao values (i);    #往test表添加数据

set i = i + 1;                    #循环一次,i加一

if i > 80 then                    #结束循环的条件: 当i大于80时跳出loop循环

leave lp;

end if;

end loop;

select * from maomao;                 #查看maomao表数据

end

//                                      #结束定义语句

call loopTest();更多免费相关学习推荐:mysql数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值