开发者社区> 问答> 正文

MySQL中的For循环示例?mysql

在MySQL中,我有一个带有For循环的存储过程:

DELIMITER $$
CREATE PROCEDURE ABC()

BEGIN DECLARE a INT Default 0 ; simple_loop: LOOP SET a=a+1; select a; IF a=5 THEN LEAVE simple_loop; END IF; END LOOP simple_loop; END $$ 它总是打印1。MySQL for循环的正确语法是什么?

展开
收起
保持可爱mmm 2020-05-16 21:57:31 531 0
1 条回答
写回答
取消 提交回答
  • drop table if exists foo; create table foo ( id int unsigned not null auto_increment primary key, val smallint unsigned not null default 0 ) engine=innodb;

    drop procedure if exists load_foo_test_data;

    delimiter # create procedure load_foo_test_data() begin

    declare v_max int unsigned default 1000; declare v_counter int unsigned default 0;

    truncate table foo; start transaction; while v_counter < v_max do insert into foo (val) values ( floor(0 + (rand() * 65535)) ); set v_counter=v_counter+1; end while; commit; end #

    delimiter ;

    call load_foo_test_data();

    select * from foo order by id;来源:stack overflow

    2020-05-16 22:02:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
MySQL 5.7让优化更轻松 立即下载
深入MySQL实战 立即下载
好的 MySQL 兼容可以做到什么程度 立即下载

相关镜像