栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 系统运维 > 数据库 > MySQL

mysql数据库如何循环语句

MySQL 更新时间:发布时间: 百科书网 趣学号

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

循环编写方式1:while关键字

delimiter //                            #定义标识符为双斜杠
drop procedure if exists whileTest;          #如果存在whileTest存储过程则删除
create procedure whileTest()                 #创建无参存储过程,名称为whileTest
begin
    declare i int;                            #申明变量
    set i = 0;                               #变量赋值
    while i < 80 do                          #结束循环的条件: 当i大于80时跳出while循环
    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数据库

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/13218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号