MySQL 循环语句

本文总结了mysql常见的三种循环方式:while、repeat和loop循环。还有一种goto,不推荐使用。

一、while循环

delimiter //                            #定义标识符为双斜杠
drop procedure if exists test;          #如果存在test存储过程则删除
create procedure test()                 #创建无参存储过程,名称为test
begin
    declare i int;                      #申明变量
    set i = 0;                          #变量赋值
    while i < 10 do                     #结束循环的条件: 当i大于10时跳出while循环
        insert into test values (i);    #往test表添加数据
        set i = i + 1;                  #循环一次,i加一
    end while;                          #结束while循环
    select * from test;                 #查看test表数据
end
//                                      #结束定义语句
call test();   

二、repeat循环

delimiter //                            #定义标识符为双斜杠
drop procedure if exists test;          #如果存在test存储过程则删除
create procedure test()                 #创建无参存储过程,名称为test
begin
    declare i int;                      #申明变量
    set i = 0;                          #变量赋值
    repeat
        insert into test values (i);    #往test表添加数据
        set i = i + 1;                  #循环一次,i加一
    until i > 10 end repeat;            #结束循环的条件: 当i大于10时跳出repeat循环
    select * from test;                 #查看test表数据
end
//                                      #结束定义语句
call test();                            #调用存储过程

三、loop循环

delimiter //                            #定义标识符为双斜杠
drop procedure if exists test;          #如果存在test存储过程则删除
create procedure test()                 #创建无参存储过程,名称为test
begin
    declare i int;                      #申明变量
    set i = 0;                          #变量赋值
    lp : loop                           #lp为循环体名,可随意 loop为关键字
        insert into test values (i);    #往test表添加数据
        set i = i + 1;                  #循环一次,i加一
        if i > 10 then                  #结束循环的条件: 当i大于10时跳出loop循环
            leave lp;
        end if; 
    end loop;
    select * from test;                 #查看test表数据
end
//                                      #结束定义语句
call test();                            #调用存储过程


--------------------- 
作者:yangzjchn 
来源:CSDN 
原文:https://blog.csdn.net/yangzjchn/article/details/82705565 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL提供了多种循环语句来实现循环操作。其中包括while循环、loop循环、repeat循环和非标准的goto循环。然而,由于goto语句的跳跃性可能会导致代码的可读性和维护性下降,因此不建议使用。\[1\] 下面是一个使用loop循环的示例存储过程: ``` delimiter $$ drop procedure if exists lopp; create procedure lopp() begin declare i int; set i = 1; lp1: LOOP insert into user_profile (uid) values (i); set i = i+1; if i > 30 then leave lp1; end if; end LOOP; end $$ ``` 另外,还可以使用while循环来实现循环操作。下面是一个使用while循环的示例存储过程: ``` delimiter // drop procedure if exists test; create procedure test() begin declare i int; set i = 0; while i < 5 do insert into test values(i+11, 'test', '20'); set i = i+1; end while; select * from test; end // call test(); delimiter ; ``` 这个存储过程使用while循环将数据插入到test表中,并在循环结束后查询表中的数据。\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [Mysql中的循环语句](https://blog.csdn.net/aihali/article/details/46622349)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [MySQL循环语句](https://blog.csdn.net/Linuxprobe18/article/details/113103194)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值