我需要从给定的星期数中找出开始和结束日期

时间:2019-06-16 17:48:14

标签: mysql

我需要创建一个名为week的表,其中包含所有星期数以及其他信息,以便我们可以正确查询,但是我想在week_start和week_end日期添加2个新字段,说明该周的开始和结束时间。当我们在mysql中使用week()函数时,我们可以传递一个额外的参数来告诉星期几开始以获取星期数,在这里我也想从星期数中找出星期几,以便进行填充根据用户需要的那两个字段。可以说我们有:

年:2019 周:25

我想找出开始和结束的日期,但是我想传递额外的参数1,2就像找出星期数一样,例如:week('2019-11-28',1 )= 48,因此为了进行相反的操作并找出该周52的第一个日期,我们还需要传递第二个参数,说明该周何时开始。那是我需要帮助的地方,以使每个人都能做到这一点。

谢谢

DROP procedure IF EXISTS `populate_weeks_table`;

DELIMITER $$
USE `whb_staging`$$
CREATE PROCEDURE `populate_weeks_table` ()
BEGIN

DECLARE year_start  INT;
DECLARE year_end INT;
DECLARE weekcount INT;
SET year_start = year(now()) - 60;
SET year_end   = year(now()) + 60;
SET weekcount = 1;

CREATE TABLE `weeks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `yearweek_num` int(11) DEFAULT NULL,
  `week_year` int(11) DEFAULT NULL,
  `week_num` int(11) DEFAULT NULL,
  `week_start` date DEFAULT NULL,
  `week_end` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `yearweek` (`yearweek_num`),
  KEY `wyear_wnum` (`week_year`,`week_num`),
  KEY `wstart_wend` (`week_start`,`week_end`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

WHILE year_start  <= year_end DO
   WHILE weekcount <= 52 DO
      insert into weeks(`yearweek_num`,`week_year`,`week_num`,`week_start`,`week_end`)
             values(concat(year_start, weekcount), year_start, weekcount, '2019-01-01', '2019-01-01');
      SET  weekcount = weekcount + 1; 
   END WHILE;
   SET  year_start = year_start + 1;
   SET  weekcount = 1;
END WHILE;

END$$

DELIMITER ;

0 个答案:

没有答案