mysql存储函数的应用

############## 基本 #############
delimiter $$
drop function if exists test$$
create function test ()
returns int
begin
return (select count(id) from test);
end$$
delimiter ;
select test();
############### 传入参数 (计算日期差) ###################
delimiter $$
drop function if exists test$$
create function test (start date,end date)
returns int
begin
declare days int;
declare next,past date;
set days=0;
set next=start+interval 1 day;
while next<=end
do
set days=days+1;
set past=next;
set next=next+interval 1 day;
end while;
return days;
end$$
delimiter ;
mysql> select test('1991-01-12','1992-01-12');
+---------------------------------+
| test('1991-01-12','1992-01-12') |
+---------------------------------+
| 365 |
+---------------------------------+
#####################################################################################################

你可能感兴趣的:(mysql,数据库,函数,应用,休闲)