MySQL编程,MySQL变量,MySQL循环 电脑版发表于:2021/8/16 17:20 >[TOC] ## mysql变量 mysql变量我们主要介绍三种 - 系统变量 - 用户自定义变量 - 局部变量 ### 系统变量 tn2>系统变量是MySQL服务器提供,不是用户定义的,属于服务器层面 **系统变量分为:** - 全局变量(global) - 会话变量(session)。 navicat新建一次查询窗口就是一个独立的会话。 #### 查看系统变量 ##### 查看会话级别的系统变量 由于系统变量很多,所以可以在查询的时候加上一个模糊匹配 ```sql /*查看系统变量。没有加级别默认是会话级别*/ show variables like 'auto%' show session variables like 'auto%' ``` 如果知道了想查询的系统变量的具体名字也可以使用如下的查询方式 ```sql select @@session.autocommit ``` ##### 查询全局级别的系统变量 ``` /* 查询全局级别的系统变量 */ -- 方法1 show global variables like 'autocommit' -- 方法2: select @@global.autocommit ``` #### 设置系统变量 ##### 设置会话变量: ```sql set session autocommit = 0 ``` ##### 设置全局变量 ```sql set global autocommit = 0 ``` ### 用户自定义变量 用户自定义变量: >- 用户根据业务逻辑自定义的变量 - 两个@是系统变量,一个@是自定义变量 - 自定义变量的作用域是当前会话 - myql中用户自定义变量是不需要先申明的可以直接赋值 #### 赋值: 下面列了4中给变量赋值的方法 ```sql -- 变量赋值 set @username = 'aa'; -- 变量赋值方法2 set @age := '22'; -- 变量赋值方法3 select @color:='red' -- 变量赋值方法4 select count(*) into @mycount from users ``` #### 查询: 直接使用select查询 ``` select @username,@age,@color,@mycount ``` ![](https://img.tnblog.net/arcimg/aojiancc2/07b8357851f64dd89fb18862edd9d403.png) #### 变量之间可以直接做运算: ``` set @result = @mycount-1; ``` ### 局部变量 局部变量: tn6>1:局部变量是根据需要定义在局部生效的变量。 2:访问之前需要使用declare申明。可以作用存储过程内的局部变量和输入参数 3:局部变量的范围是在其申明的begin,end块 #### 使用局部变量 ```sql create procedure proc_cal() begin declare count int default 0; select count(*) into count from users; set count = count+10; select count; end ``` 测试输出执行一次存储过程即可 call proc_cal(); #### 注意: 用户自定义变量当然可以在存储过程中使用 比如: ``` create procedure proc_cal() begin set @mycount = 1; select @mycount; end ``` 当时次数@mycount的变量作用域不仅仅是在begin,end块,外面也可以查询到。 ## mysql条件判断 ### if条件判断 #### 语法: ``` if 条件表达式 then ...... elseif 条件表达式 then ...... else ...... end if; ``` #### 实例,计算分数等级: ``` create procedure proc_select2() begin declare score int default 58; declare result varchar(10); if score>=85 then set result:='优秀'; elseif score>=60 then set result:='及格'; else set result:='不及格'; end if; select result; end call proc_select2(); ``` #### 实例,根据传递的参数不同进行不同的运算: 编写一个存储过程。要求三个输入参数,其中两个数参与运算,另外一个参数是运算的法则。 法则包含+,-,*。 也就是说如果传递3,6,+。那么存储过程的结果应该是9。如果传递3,6,-那么结果就是 -3。如果传递3,6,*那么存储的结果就是18。如果传递的是其他运算法则提示暂不支持该运算。 ``` /** 编写一个存储过程。要求三个输入参数,其中两个数参与运算,另外一个参数是运算的法则。 法则包含+,-,*。 也就是说如果传递3,6,+。那么存储过程的结果应该是9。如果传递3,6,-那么结果就是 -3。如果传递3,6,*那么存储的结果就是18。如果传递的是其他运算法则提示暂不支持该运算。 **/ drop procedure proc_sum create procedure proc_sum_type(in number1 int,in number2 int,in type char(1)) begin declare result int default 0 ; if type = '+' then set result = number1+number2; elseif type = '-' then set result = number1-number2; elseif type = '*' then set result = number1*number2; else select '暂不支持该运算'; end if; select result; end call proc_sum_type(6,6,'*'); ``` ### case when条件判断 xx ## mysql循环 ### while循环 mysql while循环的使用不能像在.net,java中那样直接使用 ,需要结合Mysq中的存储过程等语句来使用。 语法: ``` /** MySQL While循环语法 while 条件表达式 do ....循环的内容 end while; **/ ``` 传递一个参数进来,作为循环的次数,进行一个简单的循环次数控制 ``` -- 传递一个参数进来,作为循环的次数 drop procedure pro_total create procedure pro_total(in n int) begin -- 定义一个局部变量 declare i int default 1; while i<=n do select i; set i=i+1; end while; end; ``` 实例:求1到n的和 ``` -- 求和循环 create procedure pro_total(in n int) begin declare i int default 1; declare total int default 0; while i<=n do set total = total+i; set i = i+1; end while; select total; end -- 调用存储过程 call pro_total(10); ``` 限制循环次数的参数必须是大于0的。如果参数没有大于0 ,提示:参数无效,并且直接退出存储过程: ``` create procedure pro_total(in n int) label:begin -- 定义一个局部变量 declare i int default 1; -- 循环次数小于0,并且直接退出存储过程 if n < 1 then select '参数无效'; leave label; end if; while i<=n do select i; set i=i+1; end while; select '存储过程执行完毕'; end; call pro_total(-1) ```