函数 与存储过程区别
存储过程:可以有0-n个返回值
函数:只能有一个返回值
存储过程:增删改
函数:适合数据处理
函数创建语法
create function 函数名(参数列表) returns 返回类型
BEGIN
函数体
return ;
END
函数调用语法:
select 函数名(参数列表)
分支控制结构
if控制结构
语法:
if结构
if 条件1 THEN ;
elseif 条件2 then ;
end if
特点:只能放在 begin end 中
样例
# if测试
create function test_if(num int(11)) returns VARCHAR(10)
BEGIN
if num > 1 then return '成功';
elseif num < 1 then return '失败';
end if;
END
select test_if(0)
Switch 控制结构
语法:
case 变量|表达式|字段
when 要判断的条件 then
when 要判断的条件 then
else 要返回的值
end case
</