Mysql高级

调用:
小知识
@description : 这种变量要在变量名称前面加上“@”符号,叫做用户会话
变量,代表整个会话过程他都是有作用的,这个类似于全局变量一样。
@@global.sort_buffer_size : 这种在变量前加上 "@@" 符号, 叫做 系统变

3.6.4 case结构
语法结构 :
create procedure pro_test5(in height int , out
description varchar(100))
begin
if height >= 180 then
set description='身材高挑';
elseif height >= 170 and height < 180 then
set description='标准身材';
else
set description='一般身材';
end if;
end$
1
2
3
4
5
6
7
8
9
10
call pro_test5(168, @description)$
select @description$
1
2
3
方式一 : switch() case other
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
1
2
3
4
5
6
7
8
9
10需求:
示例 :
END CASE;
方式二 :
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
给定一个月份, 然后计算出所在的季度
输入一个数字(1~7) 根据数字判断星期几
输入一个月份 判断这个有几天2月28天。
1
2
3
4
5
delimiter $
create procedure pro_test9(month int)
begin
declare result varchar(20);
case
when month >= 1 and month <=3 then
set result = '第一季度';
when month >= 4 and month <=6 then
set result = '第二季度';
when month >= 7 and month <=9 then
set result = '第三季度';
when month >= 10 and month <=12 then
set result = '第四季度';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
153.6.5 while循环
语法结构:
需求:
示例 :
end case;
select concat('您输入的月份为 :', month , ' , 该月份为 :
' , result) as content ;
end$
delimiter ;
16
17
18
19
20
21
22
23
while search_condition do
statement_list
end while;
1
2
3
4
5
计算从1加到n的数的和值1
delimiter $
create procedure pro_test8(n int)
begin
declare total int default 0;
declare num int default 1;
while num<=n do
set total = total + num;
set num = num + 1;
end while;
select total;
end$
1
2
3
4
5
6
7
8
9
10
11
12
133.6.6 repeat结构
有条件的循环控制语句, 当满足条件的时候退出循环 。while 是满足条件才
执行,repeat 是满足条件就退出循环。
语法结构 :
需求:
示例 :
delimiter ;14
REPEAT
statement_list
UNTIL search_condition
END REPEAT;
1
2
3
4
5
6
7
计算从1加到n的值1
delimiter $
create procedure pro_test10(n int)
begin
declare total int default 0;
repeat
set total = total + n;
set n = n - 1;
until n=0
end repeat;
select total ;
end$
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
173.6.7 loop语句
LOOP 实现简单的循环,退出循环的条件需要使用其他的语句定义,通常
可以使用 LEAVE 语句实现,具体语法如下:
如果不在 statement_list 中增加退出循环的语句,那么 LOOP 语句可以用
来实现简单的死循环。
3.6.8 leave语句
用来从标注的流程构造中退出,通常和 BEGIN ... END 或者循环一起使
用。下面是一个使用 LOOP 和 LEAVE 的简单例子 , 退出循环:
delimiter ;18
[begin_label:] LOOP
statement_list
END LOOP [end_label]
1
2
3
4
5
delimiter $
CREATE PROCEDURE pro_test11(n int)
BEGIN
declare total int default 0;
ins: LOOP
IF n <= 0 then
leave ins; -- 退出指定的位置
END IF;
set total = total + n;
set n = n - 1;
END LOOP ins;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
163.6.9 游标/光标 (了解)
游标是用来存储查询结果集的数据类型 , 在存储过程和函数中可以使用游
标对结果集进行循环的处理。游标的使用包括游标的声明、OPEN、FETCH
和 CLOSE,其语法分别如下。
声明游标:
OPEN 游标:
FETCH 游标:
CLOSE 游标:
示例 :
初始化脚本:
select total;
END$
delimiter ;
17
18
19
20
21
DECLARE cursor_name CURSOR FOR select_statement ; --
select语句
1
OPEN cursor_name ;1
FETCH cursor_name INTO var_name [, var_name] ...1
CLOSE cursor_name ;1create table emp(
id int(11) not null auto_increment ,
name varchar(50) not null comment '姓名',
age int(11) comment '年龄',
salary int(11) comment '薪水',
primary key(`id`)
)engine=innodb default charset=utf8 ;
insert into emp(id,name,age,salary) values(null,'金毛狮
王',55,3800),(null,'白眉鹰王',60,4000),(null,'青翼蝠
王',38,2800),(null,'紫衫龙王',42,1800);
1
2
3
4
5
6
7
8
9
10
-- 查询emp表中数据, 并逐行获取进行展示
create procedure pro_test11()
begin
declare e_id int(11);
declare e_name varchar(50);
declare e_age int(11);
declare e_salary int(11);
declare emp_result cursor for select * from emp;
open emp_result;
fetch emp_result into e_id,e_name,e_age,e_salary;
select concat('id=',e_id , ', name=',e_name, ',
age=', e_age, ', 薪资为: ',e_salary);
fetch emp_result into e_id,e_name,e_age,e_salary;
select concat('id=',e_id , ', name=',e_name, ',
age=', e_age, ', 薪资为: ',e_salary);
fetch emp_result into e_id,e_name,e_age,e_salary;
select concat('id=',e_id , ', name=',e_name, ',
age=', e_age, ', 薪资为: ',e_salary);
fetch emp_result into e_id,e_name,e_age,e_salary;
select concat('id=',e_id , ', name=',e_name, ',
age=', e_age, ', 薪资为: ',e_salary);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22通过循环结构 , 获取游标中的数据 :
fetch emp_result into e_id,e_name,e_age,e_salary;
select concat('id=',e_id , ', name=',e_name, ',
age=', e_age, ', 薪资为: ',e_salary);
close emp_result;
end$
23
24
25
26
27
28
29
DELIMITER $
create procedure pro_test12()
begin
DECLARE id int(11);
DECLARE name varchar(50);
DECLARE age int(11);
DECLARE salary int(11);
DECLARE has_data int default 1;
DECLARE emp_result CURSOR FOR select * from emp;
-- 若没有数据返回,程序继续,并将变量has_data设为0
DECLARE EXIT HANDLER FOR NOT FOUND set has_data = 0;
open emp_result;
repeat
fetch emp_result into id , name , age , salary;
select concat('id为',id, ', name 为' ,name , ', age
为 ' ,age , ', 薪水为: ', salary);
until has_data = 0
end repeat;
close emp_result;
end$
DELIMITER ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
263.7 存储函数 ---存储过程 函数:函数有返
回值。
语法结构:
案例 :
定义一个存储函数, 请求满足条件的总记录数 ;
调用:
###
创建一种表、 id , name, age , status [必须为0或者1]
CREATE FUNCTION function_name([param type ... ])
RETURNS type
BEGIN
...
END;
1
2
3
4
5
delimiter $
create function count_city(countryId int)
returns int
begin
declare cnum int ;
select count(*) into cnum from city where country_id
= countryId;
return cnum;
end$
delimiter ;

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无脑的打码人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值