当前位置:网站首页>MySQL数据库————流程控制
MySQL数据库————流程控制
2022-08-01 18:15:00 【心随而动】
系列文章目录
文章目录
前言
SQL语言作为一个高级语言,和c语言一样,也有选择结构,只不过被叫做流程控制,作为基础语法,学好流程控制对开发也有很大的帮助。
一、顺序结构
顺序结构关键字:begin····end
,相当于{······},是SQL语言中的复合语句。
顺序结构的基本格式:
begin
sql1代码1;
sql2代码;
······
end;
二.数据库变量
1.局部变量
– 定义局部变量的基本格式:declare 变量名 数据类型;
– 变量的初始化declare 变量名 数据类型 default 初始值;
– 给变量赋值:set 变量名=变量值;
注意:局部变量只能在begin····end之间使用;
例如:
delimiter //
create procedure sum3(in x int,in y int)
begin
declare z int default 0; -- 定义一个局部变量并初始化为0;
set z=x+y; -- 给z赋值;
select z as "两数的和";
end//
delimiter ;
call sum3(123,345);
2.用户变量
使用用户变量的一般格式:set @变量名=变量值;
如:set @a=10; select @a; set @a='abc'; select @a; set @b="2000-1-20" select @b;
用户变量是弱类型,没有指定的数据类型,可以支持多种数据类型,可以理解为万用数据类型,用户变量只能在当前连接中使用,不同的数据库连接无法共用一个数据库变量,变量名相同也无法共用。下面用一组图片来解释。
第一次使用用户变量:
第二次继续使用第一次定义的用户变量:
对比两次的数据我们可以发现,第二次使用数据库的时候无法再使用第一次登录定义的用户变量,简单的来说,就是你第一次连接数据库定义的用户变量第二次连接后无法再使用。
– 用户变量应用举例:
-- 使用用户变量;
delimiter //
create procedure sum4(in x int,in y int,out z int)
begin
set z=x+y;
end//
set @a=10;
set @b=390;
call sum4(@a,@b,@c);
select @c as"两数之和"; -- 用用户变量来接收存储过程返回的值;
3.会话变量
会话变量是系统自带的,是用来保存数据库当前会话的默认数据,当前连接有效,重新连接会恢复默认值,使用会话变量的基本格式:@@会话变量名;
查看当前连接的会话变量:show session variables;
4.全局变量
和会话变量一样,全局变量也是自带的,是用来保存数据库当前服务的默认数据,当前服务有效,重启服务后恢复默认值。使用全局变量的基本格式:@@全局变量;
查看当前服务的全局变量:show global variables;
三.分支选择结构
1.if else结构
①.if语句的基本格式:if <判断条件> then <语句>; end if;
判断if之后的条件,如果条件满足则执行语句,否则不执行语句。(注意结束if需要用end if)。
②.if else语句的基本格式
if <判断条件> then <语句1>
else <语句2>;
end if;
判断条件后如果满足条件则执行语句1,否则执行语句2.
③.if else 嵌套的基本格式
if <判断条件1> then <语句1;>
else if <判断条件2> then <语句2>;
else if <判断条件3> then <语句3>;
···········
else then <语句n>
end if;
例如:对学生成绩进行分类
#分支结构的使用
delimiter //
create procedure get_grade1(in score float,out grade varchar(30))
begin
declare s int default 0;
set s=score div 10; -- 整除;
if s=9 then set grade="优秀";
elseif s=8 then set grade="优";
elseif s=7 then set grade='良';
else if s=6 then set grade="及格";
else then set grade="不及格";
end if;
end //
delimiter ;
call get_grade1(78,@ss);
select @ss;
2.case多分支结构
case when
多分支结构基本格式:
case 条件
when 值1 then 语句1;
when 值2 then 语句2;
when 值3 then 语句3;
when 值4 then 语句4;
······
else 语句n+1 #相当于default
end case;
判断case之后的条件,如果条件值于其中某个when之后的值相等,则执行when中then之后的语句,如果都不满足则执行else后面的语句。
和c语言中的switch。。case语句相似;
例如:
delimiter //
create procedure sention(in score int)
begin
declare s int default 0;
set s=score div 10;
case s
when 10 then select '满分' as 成绩等级;
when 9 then select '优秀' as 成绩等级;
when 8 then select '良好' 成绩等级;
when 7 then select '及格' 成绩等级;
when 6 then select '及格' 成绩等级;
else select '不及格' 成绩等级;
end case;
end //
delimiter ;
call sention(99);
四.循环结构
1.while·····do循环结构
while循环基本格式:while 条件表达式 do 循环语句 end while;
如果while之后的条件满足则循环执行do之后的循环体语句,直到条件不满足则结束循环。
数据库中的while循环和c语言中的while循环一样。
例如:求数字1到100的和
-- while循环
delimiter //
create procedure test(in s int,out s2 int)
begin
declare i int default 1;
declare su int default 0;
while i<=s
do
set su=su+i;
set i=i+1;
end while;
set s2=su;
end //
delimiter ;
call test(100,@n);
select @n;
2.loop循环
loop循环的结构
①.loop循环基本格式:loop 循环语句; end loop;
注意:这里的loop循环时一个无限循环(死循环),其没有结束符,所以需要手动添加结束条件;
②.给loop循环手动添加结束条件循环别名:loop 循环条件; if 条件表达式 then leave 循环别名; end if; end loop;
首先执行loop之后的循环体语句,循环体语句中的if条件满足则leave离开循环,否则继续循环;
代码演示:
-- loop循环;
-- 求1到n中的质数;
delimiter //
create procedure section(in n int)
begin
declare i int default 1;
S:loop
set i=i+1;
if i%2=0 then iterate S; -- iterate和c语言中的continue相似;
else select i;
end if;
if i>n then leave S;
end if;
end loop;
end //
delimiter ;
call section(100);
3.repeat循环
repeat循环的基本格式:repeat 循环体语句; until 条件表达式; end repeat;
首先执行repeat之后的循环体语句,然后判断until之后的条件,如果条件不满足则继续循环,否则条件满足则结束循环。#注意:repeat循环相当于c语言中的do···while循环语句,都是先执行一次循环体然后再进行条件判断,但是不同的是,do while循环是条件不满足时跳出循环,repeat循环是条件满足时才结束循环,并且until后不能有;号。
例如:求整数n到m的和;
-- repeat循环;
-- 求n到m的和;
delimiter //
create procedure sumall(in n int,in m int,out a int)
begin
declare ss int default 0;
declare i int default n;
repeat
set ss=ss+i;
set i=i+1;
set a=ss;
until i>m
end repeat;
end//
delimiter ;
call sumall(10,80,@a);
select @a;
4.跳出语句
①.leave
leave的基本格式:leave 别名;
离开别名所代表的结构,别名可以代表语句块或循环;
相当于c语言中的break语句,用于跳出整个循环,结束整个循环;
②.iterate
iterate的基本格式:iterate 别名;
相当于c语言中的continue语句,用于跳出本次循环,执行下一次的循环。
总结
本节我们学习了MySQL数据库中的流程控制,MySQL数据库的讲解基本就到这里了,下一节是关于如何将数据库和VS连接的教程。本节的代码会附在后面,大家可以去尝试理解.
代码浮现
use db_2; -- 使用数据库;
#局部变量的使用
-- 在存储过程中使用局部变量完成某些操作;
delimiter //
create procedure sum3(in x int,in y int)
begin
declare z int default 0; -- 定义一个局部变量并初始化为0;
set z=x+y; -- 给z赋值;
select z as "两数的和";
end//
delimiter ;
call sum3(123,345);
-- 使用用户变量;
delimiter //
create procedure sum4(in x int,in y int,out z int)
begin
set z=x+y;
end//
delimiter ;
set @a=10;
set @b=390;
call sum4(@a,@b,@c);
select @c as"两数之和"; -- 用用户变量来接收存储过程返回的值;
-- 会话变量的查看;
show session variables;
select @@admin_port; -- 从数据库系统中选择的会话变量;
#分支结构的使用
delimiter //
create procedure get_grade1(in score float,out grade varchar(20))
begin
declare s int default 0;
set s=score div 10; -- 整除;
if s=9 then set grade="优秀";
elseif s=8 then set grade="优";
elseif s=7 then set grade='良';
else if s=6 then set grade='及';
else set grade='不及格';
end if;
end //
delimiter ;
call get_grade1(78,@ss);
select @ss;
/*Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'then set grade="不及格"; end if; end' at line 9 */
delimiter //
create procedure sention(in score int)
begin
declare s int default 0;
set s=score div 10;
case s
when 10 then select '满分' as 成绩等级;
when 9 then select '优秀' as 成绩等级;
when 8 then select '良好' 成绩等级;
when 7 then select '及格' 成绩等级;
when 6 then select '及格' 成绩等级;
else select '不及格' 成绩等级;
end case;
end //
delimiter ;
call sention(99);
#循环结构
-- while循环
delimiter //
create procedure test(in s int,out s2 int)
begin
declare i int default 1;
declare su int default 0;
while i<=s
do
set su=su+i;
set i=i+1;
end while;
set s2=su;
end //
delimiter ;
call test(100,@n);
select @n;
-- loop循环;
-- 求1到n中的质数;
delimiter //
create procedure section(in n int)
begin
declare i int default 1;
S:loop
set i=i+1;
if i%2=0 then iterate S; -- iterate和c语言中的continue相似;
else select i;
end if;
if i>n then leave S;
end if;
end loop;
end //
delimiter ;
call section(100);
-- repeat循环;
-- 求n到m的和;
delimiter //
create procedure sumall(in n int,in m int,out a int)
begin
declare ss int default 0;
declare i int default n;
repeat
set ss=ss+i;
set i=i+1;
set a=ss;
until i>m
end repeat;
end//
delimiter ;
call sumall(10,80,@a);
select @a;
边栏推荐
- QT常用全局宏定义
- Leetcode71. 简化路径
- 【Translation】OpenMetrics cultivated by CNCF becomes an incubation project
- 【Day_11 0506】 最近公共祖先
- Zabbix6.0 DingTalk robot alarm
- 加州大学|通过图抽象从不同的第三人称视频中进行逆强化学习
- Leetcode73. Matrix Zeroing
- AntDB数据库亮相24届高速展,助力智慧高速创新应用
- EpiSci | Deep Reinforcement Learning for SoCs: Myth and Reality
- MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
猜你喜欢
随机推荐
用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
C#/VB.NET: extracted from the PDF document all form
Live chat system technology (8) : vivo live IM message module architecture practice in the system
SQL的ROUND函数用法及其实例
【无标题】setInterval和setTimeout详解
How to build a CMDB driven by consumption scenarios?
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
WinRAR | Generate multiple installers into one installer
7月30号|来一场手把手助您打造智能视觉新爆款的技术动手实验
如何让固定点的监控设备在EasyCVR平台GIS电子地图上显示地理位置?
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
B005 – 基于STC8的单片机智能路灯控制系统
使用设备树时对应的驱动编程
opencv基本的图像处理
LeetCode 0152. 乘积最大子数组:dp + 原地滚动
钳形万用表使用方法,如何测量电压、电流、电阻?
深入浅出Flask PIN
sql添加索引
How to use the Golang coroutine scheduler scheduler
三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.