日期:2014-05-16  浏览次数:20637 次

帮忙看一下这个mysql函数,用来返回两个日期之间的年数
delimiter // 
CREATE FUNCTION yeardiff (dec1 datetime,dec2 datetime) 
returns int
begin 
DECLARE Result int; 

Result = year(dec1)-year(dec2);

return (Result); 
end // 
用来返回两个日期之间的年数 想定义一个这样的函数 

就是模范oracle的datediff(‘year’,‘exp1’,'exp2')的功能

因为mysql虽然也有这样的一个函数可有局限性 只能返回两个日期之间的天数 我要返回年数 

看看我哪里有错了,错误提示
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= year(dec1)-year(dec2);

return (Result); 
end' at line 6

------解决方案--------------------
SET Result = year(dec1)-year(dec2);