mysql流程控制

-- 流程控制的基本概念
-- 数据库中的流程控制也就相当千C语言中的控制语句,其中又分为顺序结构、选择结构和循环结构三种
-- C语言中的控制结构:①顺序结构:{---}
-- ②选择结构:ifo…else…switch()…case…
-- 循环结构:for()、while()、do…while()
#在数据库中,流程控制通常与存储过程和承数配合使用。
-- MySQL中SQL语句的执行顺序:
-- 从上往下、从左至右顺序执行SQL语句。
-- 顺序结构中复合语句的关键字:begin……end;
-- begin……end相当于C语言中的大括号{……}。
-- C语言中的{}可以嵌套,如{{}}。同理,beginend也是可以嵌套的。
-- 每一个begin end相当于一个复合语句(语句块)

#begin...end嵌套 

begin
  declare x int; # int x
  set x=10;  x=10
	begin
	declare y int; int y 
	set y=11; y=11
	select x; select y;
	end
	select x;
	select y
	end 

 -- 局部变量的定义和使用
-- 1、局部变量的定义
-- 声明定义关键字:declare设置初始值关键字:default
-- 基本格式:declare变量名数据类型[default初始值]例如:declare a int default 0;
#定义了一个变量a,初始化赋值为0
-- 2、变量的赋值
-- set变量名=变量值;
-- 基本过程


delimiter //	
  begin 
     begin
		 
		 end; 
	end//
delimiter ;	
-- 局部变量的定义
-- declare 变量名 数据类型[default 初始值];
delimiter //	
begin 
     begin
		 declare n int default 0;
		 end; 
	end//
delimiter ;

delimiter //	
create procedure test1(in x int,in y int)
  begin 
     begin
		 declare n int default 0;
		 set n=x+y;
		 select n as '两个数的和为:';
		 end; 
	end//
delimiter ;
 
 call test1(10,100);

 -- 3、用户变量
-- 在用户变量的生命周期:当前连接
-- 用户变量的作用域:其定义的复合语句中,只能在begin…end中使用
-- 用户变量的定义和使用: 
-- set @变量名=变量值;
-- 例如:
-- set @x=10;
-- set @y=20;
-- set @sum = @x+@y;
-- select @x;
-- select @sum; 

 

 set @x=123;
 select @x;

-- 4、会话变量

show global variables;

有效。重启服务后恢复默认值,一般格式:@@全局变量名
-- 查看全局变量:

-- 选择结构
-- 1.if分支选择结构

delimiter//
create procedure MAXS(in x float,in y float)
begin 
    if x>y then select x;
		   elseif x<y then select y;
			     else select '大小一样';
					   end if;
 end//					
 delimier;
 call MAXS(122.56,1123.0);
 
 select 99 div 10;-- div 整除运算符
 
 select floor(95.5/10); -- foor 取整函数

 
 #循环结构
 -- while...do循环

 

 delimiter//
 create procedure test2(out sums int)
begin 
  declare s int default 0;
	declare i int default 1;
	while i<=100
	do set s=s+i;
		 set i=i+1;
		 end while;
		 set sums=s;
		 end//
delimiter;
 call test2(@sum1);
 select @sum1;

 #2. loop 循环

 delimiter//
 create procedure test3(in n int ,out sums int )
 begin
   declare s int default 0;
	declare i int default 1;
	S:loop 
		 set s=s+i;
		 set i=i+1;
		 if i>n then leave S;
		end loop;
		set sums=s;	
end// 
 delimiter;

 

/*
3、repeat循环结构①repeat循环基本格式: repeat 循环体语句; until 条件表达式 end repeat;

#注意:repeat循环相当于C语言中的dowhile循环,都是先执行一次循环体再进行条件判断
,但是不同的是,dowhile循环是条件不满足时才结束循环,而repeat是条件满足时才结束循环。并且until子句后不能有';'分号。
*/

 delimiter//
 create procedure test4(inout n int,in m int)
 begin
      declare s int default 0;
	    declare i int default 1;
		 repeat
       set s=s+i;
		   set i=i+1;
			 until i>m end repeat;
			 set n=s;
			 end//
 delimiter;
 set @sum3=1;
 call test4(@sum3,100);
select @sum3;	

  
 #4、跳转语句
-- ①leave跳转语句:用于结束整个循环,相当于C语言 的break 
-- 2iterate跳转语句:用于跳出本次循环,继续执行下次循环,相当于C语言中的continue
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酒菡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值