流程控制的概念
數據庫中的流程控制也就相當於C語言中的流程控制語句,其中有又分爲順>序結構、選擇結構和循環結構三種分支結構。
順序結構
選擇結構
循環結構
通常這些結構都與存儲過程和函數配合使用
順序結構
從上往下順序執行代碼
begin end 語句
相當於C語言中的括號 {}。C語言中的{}可以嵌套,如{ { } },begin end也是可以嵌套的
每一個begin end 相當於一個語句塊,用select 語句可以充當打印語句
定義變量 declare 變量名 類型 默認值
定義局部變量 set @變量名=表達式
使用 select 進行查詢替代打印語句
例:
begin
declare x int; ## 定義了一個int類型的變量
set x = 10; #給變量x設置值爲10
#語句塊的嵌套
begin
declare y int;
set y = x;
select x;
end
select x;
select y; #select y; 這裏可以使用變量y麼?
end
變量賦值
定義一個變量
SET語句
定義用戶變量,
#定義局部會話變量
set @變量名 #只對當前用戶使用的客戶端生效
##定義一個全局變量
set global @@變量名 #對所有客戶端生效,服務器重啓後失效
#變量名賦值操作,類型會被自動的進行識別
set 變量名=數據 #修改變量名的數據
declare語句
專門用於定義局部變量
declare 變量名 類型名
select into
使用select into語句爲變量賦值
select 列字段名 into 變量名
選擇結構
if分支結構
if語句
基本格式:
if 判斷條件 then 語句;
end if(注意結束需要用end if)
#分支語句
if (表達式) then
##執行語句
##執行語句2
##執行語句。。。
end if;
if else語句
基本格式:
if 判斷條件 then
#語句1;
else
#語句2;
end if;
if else嵌套
if語句之後的判斷條件可以用()改變條件的優先級。
多重條件語句
基本格式:
if 判斷條件1 then
## 語句1;
elseif 判斷條件2 then
## 語句2;
else
## 語句3
end if;
例如:
delimiter $$
create procedure test1(in x int)
begin
if x>100 then
select '大於100';
elseif 100=x then
select '等於100';
else
select '小於100';
end if;
end;
call test1();
delimiter ;
條件語句case
MySQL中的條件語句相當於C語言中的switch case語句。
基本格式:
case 條件值
when 值1 then 語句1;
when 值2 then 語句2;
#...
when 值n then 語句n;
else 語句n+1;
end case;
使用例子
##開關語句
##score int
case score
when 10 then select 'A';
when 20 then select 'B';
when 30 then select 'C';
else select 'D';
end case;
例如:對成績進行評分
create procedure test2(in score int)
begin
case score/10
when 10
when 9 then select '優秀';
when 8 then select '良好';
when 7
when 6 then select '及格';
else select '不及格';
end case;
end;
循環結構
在C語言中有三種循環結構:for、while和do while。
那麼在MySQL語言中也有相應的語句。
while循環語句
基本格式:
#while循環
while 條件表達式 do
#循環體語句;
end while;
數據庫中的while循環和C語言中的while幾乎一樣
例如:求數字1到10的和
create procedure test3(out sum int)
begin
declare i int default 1;
declare s int default 0;
while i<=10 do
set s = s+i;
set i = i+1;
end while;
set sum = s;
end;
call test3(@s);
select @s;
loop循環
基本格式:
#loop循環
標識符: loop
#代碼塊
end loop;
注意:這裏的loop循環是一個無限循環,其沒有結束條件,所以需要手動添加結束條件。
給loop循環添加結束條件
#有結束條件的循環
標識符: loop
#循環體語句;
if 條件表達式 then
leave 標識符; #跳出循環
end if;
end loop;
這裏給循環取別名,通過if語句判斷結束條件,leave離開跳出循環(相當於C語言中的break)。
例如:求數字1到n之和
create procedure test4(out sum int,in n int)
begin
declare s int default 0;
declare i int default 1;
sum:loop
set s=s+i;
set i=i+1;
if i>n then leave sum;
end if;
end loop;
set sum = s;
end;
repeat循環
repeat循環是條件表達式爲真時才跳出循環
基本格式
repeat
循環體語句;
#執行語句塊
until 條件表達式
end repeat;
注意:repeat循環相當於C語言中的do while循環,都是先執行一次循環體再進行條件判斷,但是不同的是,do while循環是條件不滿足時才結束循環,而repeat是條件滿足時才結束循環。並且until語句後不能有';'分號.
例如:求數字n到m之和
create procedure test5(out sum int,in n int,in m int)
begin
declare i int default n;
declare j int default n+1;
repeat set i = i+j; set j = j+1;
until j>m
end repeat;
set sum = i;
end;
call test5(@s,5,10);
select @s;
跳出循環
leave 語句
跳出並結束整個循環,相當於C語言中的break語句。
iterate
跳出並結束當本次循環,進入下次循環,相當於C語言中的continue。
注意:iterate只能在循環中使用。
在mysql語句中的跳出語句,都需要在循環語句中,寫上跳出的標識符
注意:以上所說的流程控制是相對於存儲過程而言的,流程控制大多是用於配合存儲過程的,因爲自定義函數在數據庫中使用比較少,大多數都是使用存儲過程。
havaing語句
變量是無法直接用where進行條件判斷,例如用function返回的數據起上一個別名以後
select fn(t_id) as a from [table] having a=12;
總結
流程語句的過程
變量的定義和賦值操作
用select充當打印語句
順序結構,選擇結構,循環語句
存儲過程定義語句
函數定義數據算法