• 技术文章 >数据库 >MySQL

    数据库mysql中repeat是什么

    小妮浅浅小妮浅浅2021-03-15 20:13:15原创11179

    repeat循环类似Java中的do while循环,直到条件不满足才会结束循环。

    语法:

    1

    2

    3

    4

    [别名:] REPEAT

        循环语句

    UNTIL 条件

    END REPEAT [别名]

    示例:循环打印1~10

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    delimiter //

    create procedure s_repeat()

    begin

       declare i int default 1;

       declare str varchar(256) default '1';

        # 开始repeat循环

        num:

        repeat

           set i = i + 1;

           set str = concat(str, '-', i);

       # until 结束条件

       # end repeat 结束num 结束repeat循环

       until i >= 10 end repeat num;

        # 查询字符串拼接结果

        select str;

    end //

      

    call s_repeat();

    repeat 与 while 的不同之处就是 ,while 在执行之前检查条件

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    create procedure slelect_toatal_money(IN order_id INT)

    begin

        -- 定义变量

        declare var  int;

        -- 赋值

        set var= order_id+5;

        -- repeat循环

        repeat

            select price  from oder_detail where oid = order_id;

            set var = var + 1;

            until var>7

        end repeat;

    end;

    调用示例

    1

    call slelect_toatal_money(1);

    此时会输出2组相同结果;

    1

    2

    3

    4

    price

    20

    15

    5


    以上就是在数据库mysql中repeat循环的一些用法,其中我们还和以前熟悉的while循环进行了对比,两者的区别已经有所展现和标注,小伙伴阅读的时候需要留意。

    本文教程操作环境:windows7系统、mysql5.8,DELL G3电脑。

    专题推荐:数据库mysql
    上一篇:loop在数据库mysql中进行循环 下一篇:while在数据库mysql中判断变量

    相关文章推荐

    • 数据库mysql存储中的入参出参理解• if在数据库mysql存储中判断• 数据库mysql存储中case如何转化条件?• 数据库mysql中case给成绩划分等级• null怎样在数据库mysql中变为特定值• 数据库mysql中LIMIT取值• loop在数据库mysql中进行循环

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网