####################流程控制语句
-- 1.顺序控制语句 begin..end...
delimiter //
create function max1(i int,j int)
returns int
begin
return (select * from student where 学号=xh);
end //
delimiter ;
-- 2.分支控制语句
/* if ...then...;
else....;
end if;
*/
delimiter //
create function f1(j int)
returns int
begin
declare i int;
if i%2 =0 then
return 0;
else
return 1;
end if;
end //
delimiter ;
select f1(23);
/*case语句
case....when...then...;
when...then...;
else...;
end case;
*/
delimiter //
create function scorel(i int)
returns char(6)
begin
declare j int;
declare cj char(6);
case
when j>=90 then set cj= '优秀';
when j>=80 and j<90 then set cj='良好';
when j>=70 and j<80 then set cj='中等';
when j>=60 and j<70 then set cj='及格';
else set cj='不及格';
end case;
return cj;
end //
delimiter ;
###################
#详解case when
###################
/*
CASE
WHEN 条件1 THEN 表达式1
WHEN 条件2 THEN 表达式2
...
ELSE 表达式n
END
*/
-- 例子一:
create database xscj;
use xscj;
create table xs(学号 char(6) not null primary key,
姓名 char(8) not null,
专业名 char(10) null,
性别 tinyint(1) not null default 1,
出生日期 date not null,
总学分 tinyint(1) null,
照片 blob null,
备注 text null
);
insert into xs
values( '081101', '王林',null,1,'1994-02-10',50,null,null);
insert into xs
values('081102', '程明', '计算机', 1, '1995-02-01', 50, ' D:\IMAGE\ picture.jpg', null);
insert into xs
values( '081103', '李建',null,1,'1995-02-10',50,default,default);
select 学号, 姓名,
case -- 选择的第三个列 最后别名为等级
when 总学分 is null then '尚未选课'
when 总学分 < 50 then '不及格'
when 总学分 >=50 and 总学分<=52 then '合格'
else '优秀'
end as 等级
from xs
where 专业名 = '计算机';
-- 3.循环控制语句
/*while...do
...;
end while;
*/
delimiter ##
create function sum1()
returns 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;
return s;
end##
delimiter ;