gpt4 book ai didi

mysql - MySQL中的for循环示例

转载 作者:IT老高 更新时间:2023-10-28 12:51:46 24 4
gpt4 key购买 nike

在 MySQL 中,我有一个带有 LOOP 的存储过程。 :

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 循环的正确语法是什么?

最佳答案

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 - MySQL中的for循环示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5125096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com