mysql流控制_MYSQL必备控制流程

文章目录

流程控制的概念

顺序结构

变量赋值

选择结构

循环结构

总结

流程控制的概念

数据库中的流程控制也就相当于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充当打印语句

顺序结构,选择结构,循环语句

存储过程定义语句

函数定义数据算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值