Home >Database >Mysql Tutorial >MySQL Advanced 3 - Loop Statement

MySQL Advanced 3 - Loop Statement

黄舟
黄舟Original
2016-12-29 16:31:131428browse

1. Where loop

create procedure p_addnum()
begin
declare i int default 1;
declare addresult int default 0;
while i <= 100 do
	set addresult = addresult + i;
	set i = i + 1;
end while;
select addresult;
end;
$$;

2. Repeat loop

create procedure p_updatenum()
begin
declare imin int default 1;
declare value int default 1;
declare imax int default 100;
repeat
	select value;
	set value = value + 1;
	set imin = imin + 1;
	until imin > imax
end repeat;
end;
$$;

3. Loop loop

create procedure p_updateloop()
begin
declare imin int default 1;
declare value int default 1;
declare imax int default 100;
myloop:loop
	select value;
	set value = value + 1;
	set imin = imin + 1;
	if imin > imax then
	leave myloop;
	end if;
end loop;
end;
$$;

The above is the content of MySQL Advanced Three - Loop Statement, more For related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn