存储过程示例演练

//判断姓名为"小和"的学员成绩是否通过考试,按一下格式输出: 60以下E等级 60-70 D等级 70-80 C等级  80-90 B等级 90-100 A等级

drop procedure if exists kt;

create  procedure kt()

--定义变量保存成绩

begin  

  declare cjj int;

--赋值

select cii=(

select cj from score where stuId=(

select stuId from stuinfo

where name='小和'

)

);

--case when then 语句判断

case when cj>=90 and cj <100

then  select  'A等级';

when cj >=80 and cj<90 

then select 'B等级';

when cj >=70 and cj<80 

then select 'C等级';

when cj >=60 and cj<70 

then select

'D等级';

when cj<60 

then select 'C等级';

end case;

end;

--调用存储过程

call kt();

--2.case....when的用法  1人一下的说没有,1-3人说偏少,3-4人说中等,4人以上说偏多

select address,count(cj) 人数 ,case when count(cj)<1 then '没人'

when count(cj)<3 and count(cj)>=1 then '偏少'

when count(cj)<4 and count(cj)>=3 then '中等'\

when count(cj)>=4 then '偏多'

end  '考试人数'  from stuInfo st left join score s on st.stuId=s.stuId group by address;

 

--使用存储过程打印1-50

drop procedure  if exists st;

create procedure st()

begin

--定义变量  保存初始值

declare  i int default 1;

--定义变量  拼接 结果 初始值为''

declare str varchar(200) default '' ;

--开始循环

while(i<=50)

do 

set str=concat(str,i,',');

--更新变量

set i=i+1;

end while;

--输出

select str;

end;

--调用

call st();

--3.输出1-100中5的倍数

create procedure st2()

begin

--定义变量,保存初始值

declare 1 int default 1;

--定义变量 保存拼接结果

declare str varchar(200) default '';

--开始循环

while(i<=100)

do

if(i%5==0)

then

set  str=concat(str,i,'');

end if;

--更新变量

set i=i+1;

end while;

--输出结果

select str 结果;

end;

 

call st2();

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

brid_fly

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

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

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

打赏作者

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

抵扣说明:

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

余额充值