mysql 中的判断语句(case、if)

来源:互联网 发布:人工智能的最新发展 编辑:程序博客网 时间:2024/05/19 02:44

此篇文章是通过存储过程来介绍的

1、if语句:

a、单分支 语法格式:

create procedure  p1(参数)

begin

if 表达式   then   代码1;

end if;

end;

示例:

create procedure  p1(a int)

begin

declare  i int  default 3;

if a>i then select "春天"  as  "季节" ;

end if;

end;

b、双分支语法格式

create procedure  p1(参数)

begin

if 表达式   then   代码1;

else  代码 2

end if;

end;

示例:

create procedure  p9(a int)

begin

declare  i int  default 3;

if a>i then select "春天"  as  "季节" ;
ELSE
 select "夏天"  as  "季节" ;
end if;

end;

2、Case 语句:

语法格式

create procedure  p1(参数)

begin

case 变量    when 值   then  代码1;

when 值      then  代码2;

else  代码3;

end case;

end;

示例:


create procedure  p11(a int)

begin

case a    when 1   then  select "夏"  as  "季节" ;

when 2 then        select "春天"  as  "季节" ;

else  select "秋天"  as  "季节" ;

end case;

end;




原创粉丝点击