我有一个以下的sql函数,我试图使用嵌套的if-else语句。
该功能可以正常使用嵌套'如果'声明删除。但添加后,会显示错误。
BEGIN
DECLARE daysEntitled DECIMAL(4,2);
DECLARE joinDate DATE;
DECLARE dayOfMonth INT(4);
SET joinDate = (SELECT join_date FROM users WHERE id = user);
SET daysEntitled = TIMESTAMPDIFF(MONTH, joinDate, currentDate);
IF YEAR(joinDate) = YEAR(currentDate) THEN
SET currentDate = DATE_FORMAT(currentDate,'%Y-12-31');
SET daysEntitled = TIMESTAMPDIFF(MONTH, joinDate, currentDate);
SET dayOfMonth = CAST(DAY(joinDate) AS UNSIGNED);
****** On adding this block ********
IF dayOfMonth <= 15 THEN
SET daysEntitled += 1.00;
ENDIF;
****** On adding this block ********
ELSEIF YEAR(joinDate) < YEAR(currentDate) THEN
SET daysEntitled = 8.00;
ELSEIF YEAR(joinDate) > YEAR(currentDate) THEN
SET daysEntitled = 12.00;
ELSE
SET daysEntitled = 4.00;
END IF;
RETURN daysEntitled;
END
代码似乎很好。我无法解决出错的问题。 很感谢任何形式的帮助。感谢。
答案 0 :(得分:1)
在MySQL中使用嵌套的IF
语句没有任何问题。我认为问题在于你试图增加变量的方式:
SET daysEntitled += 1.00;
^^ not allowed
请改用此代码:
IF dayOfMonth <= 15 THEN
SET daysEntitled = daysEntitled + 1.00;
ENDIF;