行业知识
mysql的判断语句
2026-11-07 14:57:43
来源:互联网转载
if
if 用于做条件判断,具体的语法结构如下,在 if 条件判断的结构中, ELSE IF 结构可以有多个,也可以没有。 ELSE 结构可以有,也可以没有。IF 条件1 THEN.....ELSEIF 条件2 THEN -- 可选.....ELSE -- 可选.....END IF;
案例
create procedure p3()begindeclare score int default 58;declare result varchar(10);if score >= 85 then set result := '优秀';elseif score >= 60 then set result := '及格';else set result := '不及格';end if;select result;end;call p3();
case
如果判定条件有多个,多个条件之间,可以使用 and 或 or 进行连接。
方法一
-- 含义: 当case_value的值为 when_value1时,执行statement_list1,当值为 when_value2时,执行statement_list2, 否则就执行 statement_listCASE case_valueWHEN when_value1 THEN statement_list1[ WHEN when_value2 THEN statement_list2] ...[ ELSE statement_list ]END CASE;
方法二
-- 含义: 当条件search_condition1成立时,执行statement_list1,当条件search_condition2成立时,执行statement_list2, 否则就执行 statement_listCASEWHEN search_condition1 THEN statement_list1[WHEN search_condition2 THEN statement_list2] ...[ELSE statement_list]END CASE;
案例
create procedure p6(in month int)begindeclare result varchar(16);casewhen month >= 1 and month = 4 and month = 7 and month = 10 and month 0 doset total := total + n;set n := n - 1;end while;select total;end;call p7(100);
repeat
repeat 是有条件的循环控制语句 , 当满足 until 声明的条件的时候,则退出循环-- 先执行一次逻辑,然后判定UNTIL条件是否满足,如果满足,则退出。如果不满足,则继续下一次循环REPEAT SQL逻辑... UNTIL 条件END REPEAT;
案例
-- A. 定义局部变量, 记录累加之后的值;-- B. 每循环一次, 就会对n进行-1 , 如果n减到0, 则退出循环create procedure p8(in n int)begindeclare total int default 0;repeatset total := total + n;set n := n - 1;until n leave xxcreate procedure p9(in n int)begindeclare total int default 0;sum:loopif n
上一篇:功率服务器怎么计算?