今天小編給大家分享一下MySQL流程控制之while、repeat、loop循環實例分析的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
創新互聯建站長期為上千多家客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為北安企業提供專業的網站設計制作、成都網站制作,北安網站改版等技術服務。擁有10多年豐富建站經驗和眾多成功案例,為您定制開發。
循環是一段在程序中只出現一次,但可能會連續運行多次的代碼。
循環中的代碼會運行特定的次數,或者是運行到特定條件成立時結束循環。
循環分類:
while
repeat
loop
循環控制:
leave類似于 break,跳出,結束當前所在的循環
iterate類似于 continue,繼續,結束本次循環,繼續下一次
【標簽:】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
循環體;
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
循環體;
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流程控制之while、repeat、loop循環實例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注創新互聯行業資訊頻道。
文章標題:MySQL流程控制之while、repeat、loop循環實例分析
本文網址:http://m.newbst.com/article44/pjcghe.html
成都網站建設公司_創新互聯,為您提供App開發、微信公眾號、網站導航、服務器托管、網站營銷、外貿網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯