码迷,mamicode.com
首页 > 数据库 > 详细

在MySQL数据库中使用判断条件

时间:2017-05-23 01:20:24      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:cas   end   efault   exit   blog   money   out   handler   l数据库   

  【根据学生编号和学生成绩判断学生成绩等级A90 B80 C70 D60 E60下】

  Select 字段列表 into 变量列表 from 表 where 条件  将某条记录中的字段列表中的值存入到变量列表中,变量列表中的变量一定是提前定义的。

/* 存储过程if控制结构 */

delimiter //

create procedure marks_level

(

    in stu_no int(4),

    in cla_no int(4),

    out _level varchar(4)

)

begin

  declare _mark int(4) default 0;

  select mark into _mark from marks where sid=stu_no and cid = cla_no;

  if _mark >= 90 then

    set _level = "A";

  elseif _mark>= 80 then

    set _level = "B";

  elseif _mark >= 70 then

    set _level = "C";

  elseif _mark >= 60 then

    set _level = "D";

  else

    set _level = "E";

  end if;

end;//

set @result = "";//

call marks_level(1,1,@result);//

select @result;//
 

 

/* 存储过程case控制结构 */
delimiter //
create procedure marks_level_case
(
    in stu_no int(4),
    in cla_no int(4),
    out _level varchar(4)
)
begin
  declare _mark int(4) default 0;
  select mark into _mark from marks where sid=stu_no and cid = cla_no;
  case
    when _mark >= 90 then
        set _level = "A";
    when _mark>= 80 then
        set _level = "B";
    when _mark >= 70 then
        set _level = "C";
    when _mark >= 60 then
        set _level = "D";
    else
        set _level = "E";
  end case;
end;//
set @result = "";//
call marks_level_case(1,1,@result);//
select @result;//

 

 /* 存储过程while控制结构 */
delimiter //
create procedure sums_while
(
    in n int(4),
    inout sums int(4)
)
begin
    while n>0 do
        set sums = sums + n;
        set n = n-1;
    end while;
end;//
set @result=0;
call sums_while(10,@result);
select @result;

 

delimiter //
create procedure sums_repeat
(
    in n int(4),
    inout sums int(4)
)
begin
    repeat
        if n>0 then
            set sums = sums + n;
        end if;
        set n = n-1;
    until n<0
    end repeat;
end;//
set @result=0;
call sums_repeat(10,@result);
select @result;

 

 

 /* 存储过程游标控制结构 */
delimiter //
create procedure pro_cursor()
begin
    declare stu_no int(11) default 0;
    declare cla_mark int(11) default 0;
    declare tag int(11) default 0;
    declare moneys int(11) default 0;
    declare cur1 cursor  for 
    select s.sid,m.mark from student s left join marks m on s.sid = m.sid;
    declare exit handler for sqlstate "02000" set tag =1;
    open cur1;
    while !tag do
        fetch cur1 into stu_no,cla_mark;
        if cla_mark >= 90 then
            set moneys = 500;
        elseif cla_mark >=80 then
            set moneys = 400;
        elseif cla_mark >=70 then
            set moneys = 300;
        else
            set moneys = 100;
        end if;
        update student set moneys=moneys where sid=stu_no;
    end while;
    close cur1;
end;//

 

在MySQL数据库中使用判断条件

标签:cas   end   efault   exit   blog   money   out   handler   l数据库   

原文地址:http://www.cnblogs.com/shuo-128/p/6892146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!