MariaDB星期开始日期&第1周至第52周

时间:2016-06-07 19:23:20

标签: sql stored-procedures mariadb

我已经编写了一个存储过程来获取该日期的一周,它还会返回一周开始的日期以及周数和年份。

我知道周末'函数,但这并没有给我一周开始的日期,而且我不知道在一周和一年中这样做的函数。

问题是:
如何获得“约会”日期?在一周的开始给出周数?将周开始作为日期索引传递的地方,0 =星期日,1 =星期一等。

我目前的功能并不总是有效,如果一周的第一天是星期一,那么星期日会进入下一周,而不是我想要的同一周结束。

4 个答案:

答案 0 :(得分:1)

我也在对此进行了一些挖掘。但是我偶然发现了一些同样有效的mysql代码。它基本上是根据星期几减去几天。也就是说,如果日期是星期三(4),则您知道该日期是1-4 = -3天前。

如何?

# with Sunday being the start of the week:
select convert(date_add(now(), interval(1-dayofweek(now())) day), date) as WeekStartDate
select convert(date_add(now(), interval(7-dayofweek(now())) day), date) as WeekEndDate

# with Monday being the start of the week:
select convert(date_add(now(), interval(2-dayofweek(now())) day), date) as WeekStartDate
select convert(date_add(now(), interval(8-dayofweek(now())) day), date) as WeekEndDate

信用: How do I get the first day of the week of a date in mysql?

答案 1 :(得分:0)

使用Sequence引擎。您可以根据需要调整以下示例:

MariaDB [_]> SHOW ENGINES\G
.
.
.
*************************** 3. row ***************************
      Engine: SEQUENCE
     Support: YES
     Comment: Generated tables filled with sequential values
Transactions: YES
          XA: NO
  Savepoints: YES
.
.
.

MariaDB [_]> SET @`year` := 2016,
          ->     @`mode` := 1,
          ->     @`week` := 23;
Query OK, 0 rows affected (0.00 sec)

MariaDB [_]> SELECT
          ->   `der`.`date`,
          ->   `der`.`week`,
          ->   `der`.`year`
          -> FROM (
          ->   SELECT
          ->     `der`.`date`,
          ->     WEEK(`der`.`date`, @`mode`) `week`,
          ->     YEAR(`der`.`date`) `year`
          ->   FROM (
          ->     SELECT
          ->       DATE_ADD(CONCAT(@`year`, '-01-01'), INTERVAL `s`.`seq` DAY) `date`
          ->     FROM
          ->       seq_0_to_365 `s`
          ->   ) `der`
          -> ) `der`
          -> WHERE
          ->   `der`.`week` = @`week` AND
          ->   `der`.`year` = @`year`;
+------------+------+------+
| date       | week | year |
+------------+------+------+
| 2016-06-06 |   23 | 2016 |
| 2016-06-07 |   23 | 2016 |
| 2016-06-08 |   23 | 2016 |
| 2016-06-09 |   23 | 2016 |
| 2016-06-10 |   23 | 2016 |
| 2016-06-11 |   23 | 2016 |
| 2016-06-12 |   23 | 2016 |
+------------+------+------+
7 rows in set (0.01 sec)

答案 2 :(得分:0)

解决了,我重写了存储过程:

    exitProc:BEGIN
    #--
    # Procedure:
    #   weekFromDate
    #
    # Parameters:
    #   vcCompKey, the key associated with the company
    #   dtDate, the date to translate
    #   dtOutSOW, returned start of week date
    #   siOutWeek, returned week number
    #   siOutYear, returned year
    #--
        DECLARE siDIY          SMALLINT;   #Day in year
        DECLARE siFDOW         SMALLINT;   #First day of week
        DECLARE siGoBack       SMALLINT;      #Flag used to check for last year
        DECLARE siRmonth       SMALLINT;   #Reference Month
        DECLARE siRyear        SMALLINT;   #Reference Year
        DECLARE dtSOY          DATE;       #Date of start of year
        DECLARE vcFMDOY        VARCHAR(12);#First month and day of year
        DECLARE vcFDOW         VARCHAR(12);#First day of the week
        DECLARE vcDYSOW        VARCHAR(80);#Days of week
    #Get the first day of the week for the specified company
        SET vcFDOW = vcGetParamValue(vcCompKey, 'Var:First day of week');

    IF (vcFDOW IS NULL) THEN
    #No entry found, abort! 
        LEAVE exitProc;
    END IF;
    #Get the first month and day of the year for the specified company
    SET vcFMDOY = vcGetParamValue(vcCompKey, 'Var:First day of year');  

    IF (vcFMDOY IS NULL) THEN
    #No entry found, abort! 
        LEAVE exitProc;
    END IF;
    #Set-up days of week
    SET vcDYSOW = 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday';   
    #Get the first day of the week index base 1
    SET siFDOW = FIND_IN_SET(LOWER(vcFDOW), LOWER(vcDYSOW)) - 1;
    #Get the reference month and year   
    SET siRmonth = MONTH(dtDate);
    SET siRyear = YEAR(dtDate);
    SET dtSOY = DATE(CONCAT(siRyear, '/', vcFMDOY)); 
    #Calculate the start of week date
    SET dtOutSOW = DATE_SUB(dtDate, INTERVAL (DAYOFWEEK(dtDate) - siFDOW) DAY) + 1;
    #Calculate the day in year
    SET siDIY = DATEDIFF(dtOutSOW, dtSOY);
    #Do we need to go back to the end of the previous year? 
    SET siGoBack = YEAR(dtDate) - YEAR(dtOutSOW);

    IF siGoBack < 0 Or siDIY < 0 Or dtDate < dtOutSOW THEN
    #Yes
        IF YEAR(dtOutSOW) = YEAR(dtDate) THEN
            SET dtOutSOW = DATE_SUB(dtOutSOW, INTERVAL 7 DAY);
        END IF;
        SET dtSOY = DATE(CONCAT(YEAR(dtOutSOW), '/', vcFMDOY)); 
        SET siDIY = DATEDIFF(dtOutSOW, dtSOY);
    END IF;     
    #Calculate the week no. and year
    SET siOutWeek = (siDIY / 7) + 1;
    SET siOutYear = YEAR(dtOutSOW); 
END

此例程确实利用了我的数据库中的其他表,并允许公司具有不同的年份。

答案 3 :(得分:0)

作为测试,我会找到本周的开始,首先注意:

mysql> SELECT NOW(), WEEK(NOW());
+---------------------+-------------+
| NOW()               | WEEK(NOW()) |
+---------------------+-------------+
| 2016-06-18 12:10:58 |          24 |
+---------------------+-------------+

然后这是功能的核心:

mysql> SELECT '2016-01-01'
             + INTERVAL 7*24
             - DAYOFWEEK('2016-01-01')
             + 1 DAY;
+----------------------------------------------------------------+
| '2016-01-01' + INTERVAL 7*24 - DAYOFWEEK('2016-01-01') + 1 DAY |
+----------------------------------------------------------------+
| 2016-06-12                                                     |
+----------------------------------------------------------------+

'2016-01-01'是今年的开始。
24WEEK()号码 + 1 DAY是为了弥补一周的开始 还需要做一些其他事情来处理一周开始一周的选择。

相关问题