对于MySQL中的循环例子 [英] For loop example in MySQL

查看:116
本文介绍了对于MySQL中的循环例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  DELIMITER $$ 
CREATE程序ABC()

BEGIN
DECLARE一个INT默认值为0;
simple_loop:LOOP
SET a = a + 1;
选择一个;
IF a = 5 THEN
LEAVE simple_loop;
END IF;
END LOOP simple_loop;
END $$

总是打印 1 。什么是MySQL for循环的正确语法?

解决方案

  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
$ b declare v_max int unsigned default 1000;
declare v_counter int unsigned default 0;

truncate table foo;
开始交易;
,而v_counter< v_max do
插入到foo(val)值(floor(0 +(rand()* 65535)));
set v_counter = v_counter + 1;
结束;
commit;
结束#

分隔符;

调用load_foo_test_data();

select * from foo order by id;


In MySQL, I have this stored procedure with a For loop in it:

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 $$

It always prints 1. What is the correct syntax for a MySQL for loop?

解决方案

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;

这篇关于对于MySQL中的循环例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆