您的位置:首页 > 数据库 > MySQL

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

2017-10-29 15:31 459 查看
此篇文章是通过存储过程来介绍的

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐