mysql自定义函数的使用
一.创建
create function 函数名 ([参数列表]) returns 数据类型
begin sql语句;
return 值;
end;
例:create function getstatus (name varchar(15),多个参数用,隔开) return int ;
begin
declare c int; //定义一个int类型的变量c
//下面这句不一定是sql语句,可以是其他的,如if-else,set等
select id from class where cname=name into c;//在这里面可以调用其他方法,如SUBSTRING(str,end);
return c;
end;
二.使用:
select getstatus("python");
或者可以当参数调用,如select * from user where status= getstatus(“status”);