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

WBOY
發布: 2022-08-09 20:22:12
轉載
1765 人瀏覽過

這篇文章為大家帶來了關於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('user-',i),'123456');
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('user-',i),'123456');
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('user-',i),'123456');
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('user-',i),'123456');
set i = i + 1;
until i > insertCount
end repeat label;
select '循环结束';
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('user-',i),'123456');
set i = i + 1;
if i > 5
then
leave label;
end if;
end loop label;
select '循环结束';
end $$
delimiter ;
call proc19_loop(10);
登入後複製

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

推薦學習:

mysql影片教學

#

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

相關標籤:
來源:jb51.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!