mysql for_MySQL中的For循环示例

MySQL中的while循环语法示例:

delimiter //

CREATE procedure yourdatabase.while_example()

wholeblock:BEGIN

declare str VARCHAR(255) default '';

declare x INT default 0;

SET x = 1;

WHILE x <= 5 DO

SET str = CONCAT(str,x,',');

SET x = x + 1;

END WHILE;

select str;

END//

哪些打印:

mysql> call while_example();

+------------+

| str        |

+------------+

| 1,2,3,4,5, |

+------------+

MySQL中的REPEAT循环语法示例:

delimiter //

CREATE procedure yourdb.repeat_loop_example()

wholeblock:BEGIN

DECLARE x INT;

DECLARE str VARCHAR(255);

SET x = 5;

SET str = '';

REPEAT

SET str = CONCAT(str,x,',');

SET x = x - 1;

UNTIL x <= 0

END REPEAT;

SELECT str;

END//

哪些打印:

mysql> call repeat_loop_example();

+------------+

| str        |

+------------+

| 5,4,3,2,1, |

+------------+

MySQL中的FOR循环语法示例:

delimiter //

CREATE procedure yourdatabase.for_loop_example()

wholeblock:BEGIN

DECLARE x INT;

DECLARE str VARCHAR(255);

SET x = -5;

SET str = '';

loop_label: LOOP

IF x > 0 THEN

LEAVE loop_label;

END IF;

SET str = CONCAT(str,x,',');

SET x = x + 1;

ITERATE loop_label;

END LOOP;

SELECT str;

END//

哪些打印:

mysql> call for_loop_example();

+-------------------+

| str               |

+-------------------+

| -5,-4,-3,-2,-1,0, |

+-------------------+

1 row in set (0.00 sec)

进行本教程:http : //www.mysqltutorial.org/stored-procedures-loop.aspx

如果我发现您将这种MySQL for循环构造推入生产,那么我将用泡沫导弹发射器向您射击。您可以使用管扳手敲打钉子,但是这样做会使您看起来很傻。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值