在MySQL資料庫中使用判斷條件

來源:互聯網
上載者:User

標籤: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資料庫中使用判斷條件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.