MySQL过程DATE变量操作

时间:2017-09-27 09:53:45

标签: mysql stored-procedures plsql

我是MySQL的新手,我在MySQL程序中有一个日期变量,我正在尝试找到该日期变量的周开始日期。我的程序如下,

CREATE PROCEDURE my_proc (IN week_start_num INT, IN my_date DATE)
BEGIN
   DECLARE my_new_date DATE;
   #I know what I'm trying here is wrong
   SET my_new_date=startdate - (INTERVAL WEEKDAY( startdate  ) - week_start_num + IF( WEEKDAY( startdate  ) > week_start_num, 0, 7 ))

   #rest of my codes goes here

END

我知道这是错的'SET my_new_date = startdate - (INTERVAL WEEKDAY(startdate) - week_start_num + IF(WEEKDAY(startdate)&gt; week_start_num,0,7))',完成它的正确方法是什么?< / p>

1 个答案:

答案 0 :(得分:1)

试试这个:

SET my_new_date = startdate
                  - INTERVAL 
                  ( 
                    WEEKDAY(startdate)
                    - week_start_num
                    + IF(WEEKDAY(startdate) > week_start_num, 0, 7)
                  )
                  DAY;