MySQL存儲(chǔ)過程之流程控制while,repeat,loop循環(huán)
前言
- 循環(huán)是一段在程序中只出現(xiàn)一次,但可能會(huì)連續(xù)運(yùn)行多次的代碼。
- 循環(huán)中的代碼會(huì)運(yùn)行特定的次數(shù),或者是運(yùn)行到特定條件成立時(shí)結(jié)束循環(huán)。
循環(huán)分類:
- while
- repeat
- loop
循環(huán)控制:
leave 類似于 break,跳出,結(jié)束當(dāng)前所在的循環(huán)
iterate類似于 continue,繼續(xù),結(jié)束本次循環(huán),繼續(xù)下一次
while循環(huán)
【標(biāo)簽:】while 循環(huán)條件 do 循環(huán)體; end while【 標(biāo)簽】;
-- 創(chuàng)建測(cè)試表 create table user ( uid int primary_key, username varchar ( 50 ), password varchar ( 50 ) );
-- -------存儲(chǔ)過程-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);
存儲(chǔ)過程語法是固定的:delimiter $$ create peocedure 循環(huán)名(參數(shù))begin 代碼 end $$ delimiter;
注意在寫循環(huán)體的時(shí)候,必須要要有定義循環(huán)的初識(shí)變量,采用declare i int default 默認(rèn)值
然后就是dlabel:while 判斷條件 do 循環(huán)體 end while label; end && 必須要有
-- -------存儲(chǔ)過程-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);
如果在內(nèi)部需要跳出循環(huán)的話,采用if 判斷 ,但是最后需要end if 結(jié)尾
這里的leave就是 跳出循環(huán),相對(duì)于break
-- -------存儲(chǔ)過程-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 相對(duì)于continue 遇到就不執(zhí)行下面的代碼
repeat循環(huán)
[標(biāo)簽:]repeat 循環(huán)體; until 條件表達(dá)式 end repeat [標(biāo)簽];
-- -------存儲(chǔ)過程-循環(huán)控制-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 '循環(huán)結(jié)束'; end $$ delimiter ; call proc18_repeat(100);
這個(gè)相對(duì)于是,無論如何都會(huì)執(zhí)行一次的循環(huán),然后是在內(nèi)部進(jìn)行判斷,如果滿足了就直接跳出
loop循環(huán)
[標(biāo)簽:] loop 循環(huán)體; if 條件表達(dá)式 then leave [標(biāo)簽]; end if; end loop;
-- -------存儲(chǔ)過程-循環(huán)控制-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 '循環(huán)結(jié)束'; end $$ delimiter ; call proc19_loop(10);
這個(gè)和repeat不同的是,需要執(zhí)行之后,利用leave 跳出循環(huán),無論是使用哪種都可以達(dá)到我們需要的效果,但是在業(yè)務(wù)中的應(yīng)用場(chǎng)景,while還是相對(duì)比較的多。
到此這篇關(guān)于MySQL存儲(chǔ)過程之流程控制while,repeat,loop循環(huán)的文章就介紹到這了,更多相關(guān)MySQL存儲(chǔ)過程 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL中Order By多字段排序規(guī)則代碼示例
這篇文章主要介紹了MySQL中Order By多字段排序規(guī)則代碼示例,小編覺得挺不錯(cuò)的,這里給大家分享下,需要的朋友可以參考。2017-10-10mysql數(shù)據(jù)庫插入速度和讀取速度的調(diào)整記錄
由于項(xiàng)目變態(tài)需求;需要在一個(gè)比較短時(shí)間段急劇增加數(shù)據(jù)庫記錄(兩三天內(nèi),由于0增加至4億)。在整個(gè)過程調(diào)優(yōu)過程非常艱辛2012-07-07MySQL入門(三) 數(shù)據(jù)庫表的查詢操作【重要】
本節(jié)比較重要,對(duì)數(shù)據(jù)表數(shù)據(jù)進(jìn)行查詢操作,其中可能大家不熟悉的就對(duì)于INNER JOIN(內(nèi)連接)、LEFT JOIN(左連接)、RIGHT JOIN(右連接)等一些復(fù)雜查詢。 通過本節(jié)的學(xué)習(xí),可以讓你知道這些基本的復(fù)雜查詢是怎么實(shí)現(xiàn)的,,需要的朋友可以參考下2018-07-07CentOS7環(huán)境下安裝MySQL5.5數(shù)據(jù)庫
大家好,本篇文章主要講的是CentOS7環(huán)境下安裝MySQL5.5數(shù)據(jù)庫,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12深入探尋mysql自增列導(dǎo)致主鍵重復(fù)問題的原因
前幾天開發(fā)的同事反饋一個(gè)利用load data infile命令導(dǎo)入數(shù)據(jù)主鍵沖突的問題,分析后確定這個(gè)問題可能是mysql的一個(gè)bug,這里提出來給大家分享下。以免以后有童鞋遇到類似問題百思不得其解,難以入眠,哈哈。2014-08-08mysql安全啟動(dòng)腳本mysqld_safe詳細(xì)介紹
這篇文章主要介紹了mysql安全啟動(dòng)腳本mysqld_safe詳細(xì)介紹,mysqld_safe增加了一些安全特性,需要的朋友可以參考下2014-07-07mysql的存儲(chǔ)過程、游標(biāo) 、事務(wù)實(shí)例詳解
這篇文章主要介紹了mysql的存儲(chǔ)過程、游標(biāo) 、事務(wù)實(shí)例詳解的相關(guān)資料,這里舉實(shí)例說明MySQL 存儲(chǔ)過程與游標(biāo)和事務(wù),需要的朋友可以參考下2017-08-08Mysql數(shù)據(jù)庫表中為什么有索引卻沒有提高查詢速度
你有沒有想起過為什么明明再數(shù)據(jù)庫中有索引,但是查詢速度卻并沒有希望的那樣快?本篇文章將帶給你答案,跟小編一起看看吧2022-02-02SQL Server COALESCE函數(shù)詳解及實(shí)例
這篇文章主要介紹了SQL Server COALESCE函數(shù)詳解及實(shí)例的相關(guān)資料,COALESCE函數(shù)比ISNULL更加強(qiáng)大,這個(gè)函數(shù)的確非常有用,需要的朋友可以參考下2016-12-12Mysql中order by、group by、having的區(qū)別深入分析
本篇文章是對(duì)Mysql中order by、group by、having的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06