而在MySQL中循环使用if语句

时间:2012-11-12 17:06:56

标签: mysql if-statement while-loop

由于最后的while循环,我得到了一个语法错误。我试过切换它但是无法在这里找到问题 - 纯粹是MySQL不支持while循环中的if语句吗?

DELIMITER //
CREATE PROCEDURE bLAHbLAHbLAH(p_fromdate datetime, p_todate datetime)
BEGIN

Declare startday datetime;
declare numberofdays int;
declare Counter int;

Set startday = p_fromdate;
Set numberofdays = (select datediff("2012-05-01","2012-11-01"));
Set Counter = 0;

WHILE startday<p_todate DO
    if (select date_format(startday, '%W')) in ('Monday','Tuesday','Wednesday','Thursday','Friday') THEN
        SET Counter = Counter+1;
        SET startday = date_add(startday, INTERVAL 1 DAY);
    else
        SET startday = date_add(startday, INTERVAL 1 DAY);
END WHILE;

select Counter; 

END//

DELIMITER ;

1 个答案:

答案 0 :(得分:2)

你忘记了

end if;
end while

之前

WHILE startday<p_todate DO
    if (select date_format(startday, '%W')) in ('Monday','Tuesday','Wednesday','Thursday','Friday') THEN
        SET Counter = Counter+1;
        SET startday = date_add(startday, INTERVAL 1 DAY);
    else
        SET startday = date_add(startday, INTERVAL 1 DAY);
    end if;                                                   /*add this line*/
END WHILE;