首頁  >  文章  >  資料庫  >  MySQL流程控制之while、repeat、loop循環

MySQL流程控制之while、repeat、loop循環

WBOY
WBOY轉載
2022-07-27 17:38:121867瀏覽

這篇文章為大家帶來了關於mysql的相關知識,主要介紹了MySQL預存程序之流程控制while、repeat、loop循環,循環中的程式碼會運行特定的次數,或者是運行到特定條件成立時結束循環,下面一起來看一下,希望對大家有幫助。

MySQL流程控制之while、repeat、loop循環

推薦學習:mysql影片教學

#前言

  • 循環是一段在程式中只出現一次,但可能會連續運行多次的程式碼。
  • 循環中的程式碼會運行特定的次數,或者是運行到特定條件成立時結束循環。

循環分類:

  • #while
  • ##repeat
  • loop

#迴圈控制:

##leave

類似 break,跳出,結束目前所在的循環

iterate

類似 continue,繼續,結束本次循環,繼續下一次while循環

【标签:】while 循环条件 do
循环体;
end while【 标签】;
-- 创建测试表
create table user (
uid int primary_key,
username varchar ( 50 ),
password varchar ( 50 )
);
-- -------存储过程-while
delimiter $$
create procedure proc16_while1(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
insert into user(uid,username,`password`) values(i,concat(&#39;user-&#39;,i),&#39;123456&#39;);
set i=i+1;
end while label;
end $$
delimiter ;
call proc16_while(10);

預存程序語法是固定的:

delimiter $$ create peocedure 循環名稱(參數)begin 程式碼end $$ delimiter;注意在寫循環體的時候,必須要有定義循環的初識變量,採用declare i int default 默認值

然後就是dlabel:

while 判斷條件do 循環體 end while label; end && 必須要有

-- -------存储过程-while + leave
truncate table user;
delimiter $$
create procedure proc16_while2(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
insert into user(uid,username,`password`) values(i,concat(&#39;user-&#39;,i),&#39;123456&#39;);
if i=5 then leave label;
end if;
set i=i+1;
end while label;
end $$
delimiter ;
call proc16_while2(10);
如果在內部需要跳出循環的話,採用if 判斷,但是最後需要end if 結尾

這裡的leave就是跳出循環,相對於break

-- -------存储过程-while+iterate
truncate table user;
delimiter $$
create procedure proc16_while3(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
set i=i+1;
if i=5 then iterate label;
end if;
insert into user(uid,username,`password`) values(i,concat(&#39;user-&#39;,i),&#39;123456&#39;);
end while label;
end $$
delimiter ;
call proc16_while3(10);

這裡的iterate 相對於continue 遇到就不執行下面的程式碼

repeat循環

[标签:]repeat
循环体;
until 条件表达式
end repeat [标签];
-- -------存储过程-循环控制-repeat
use mysql7_procedure;
truncate table user;
delimiter $$
create procedure proc18_repeat(in insertCount int)
begin
declare i int default 1;
label:repeat
insert into user(uid, username, password) values(i,concat(&#39;user-&#39;,i),&#39;123456&#39;);
set i = i + 1;
until i > insertCount
end repeat label;
select &#39;循环结束&#39;;
end $$
delimiter ;
call proc18_repeat(100);

這個相對於是,無論如何都會執行一次的循環,然後是在內部進行判斷,如果滿足了就直接跳出

loop循環

[标签:] loop
循环体;
if 条件表达式 then
leave [标签];
end if;
end loop;
-- -------存储过程-循环控制-loop
truncate table user;

delimiter $$
create procedure proc19_loop(in insertCount int)
begin
declare i int default 1;
label:loop
insert into user(uid, username, password) values(i,concat(&#39;user-&#39;,i),&#39;123456&#39;);
set i = i + 1;
if i > 5
then
leave label;
end if;
end loop label;
select &#39;循环结束&#39;;
end $$
delimiter ;
call proc19_loop(10);

這個和repeat不同的是,需要執行之後,利用leave跳出循環,無論是使用哪一種都可以達到我們需要的效果,但是在業務中的應用場景,while還是相對比較的多。

推薦學習:

mysql影片教學

#

以上是MySQL流程控制之while、repeat、loop循環的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:jb51.net。如有侵權,請聯絡admin@php.cn刪除