0

我正在使用 MySQL 程序根据两个不同的表来获取商店的营业时间和关闭时间。正常的“shop_hours”定义为开/关时间和星期几(1-7)。为了允许自定义正常的开/关时间,我还有一个名为“shop_hours_special”的表,这是用一年中的某一天列定义的。

如果我运行参数CALL timesDate('2013-01-02','1', 'false'),即使商店在这个日期营业,我也会得到 NULL 结果。

如果任何 MySQL 策划者可以给我提示如何使其在当前以外的其他年份工作,我将不胜感激!

DELIMITER $$
DROP PROCEDURE IF EXISTS timesDate$$

CREATE PROCEDURE timesDate(IN searchedDate DATE, IN dType TINYINT(3), IN ignoreCurrentTime BOOLEAN)
BEGIN
    DECLARE final_o_time, final_c_time, o_time, c_time, curTime TIME;  
    DECLARE dayYear, dayWeek, curDay INT;
    DECLARE special_exist BOOLEAN;

    SET dayYear = DAYOFYEAR(searchedDate);
    SET dayWeek = WEEKDAY(searchedDate) + 1;
    SET curDay = DAYOFYEAR(NOW());
    SET curTime = TIME(NOW());

    SELECT IF(COUNT(*) > 0, TRUE, FALSE), open_time, close_time INTO special_exist, o_time, c_time
        FROM shop_hours_special
        WHERE day_of_year = dayYear AND `type` = dType;

    IF special_exist THEN
        IF IgnoreCurrentTime THEN
            SET final_o_time = o_time;
            SET final_c_time = c_time;
        ELSEIF dayYear < curDay THEN
            SET final_o_time = NULL;
            SET final_c_time = NULL;
        ELSEIF dayYear = curDay THEN
            IF curTime < o_time THEN
                SET final_o_time = o_time;
                SET final_c_time = c_time;
            ELSEIF curTime < c_time THEN
                SET final_o_time = curTime;
                SET final_c_time = c_time;
            ELSE
                SET final_o_time = NULL;
                SET final_c_time = NULL;
            END IF;
        ELSE
            SET final_o_time = o_time;
            SET final_c_time = c_time;
        END IF;
    ELSE
        SELECT open_time, close_time INTO o_time, c_time
        FROM shop_hours
        WHERE day_of_week = dayWeek AND (open_time != MAKETIME(0,0,0) OR close_time != MAKETIME(0,0,0)) AND `type` = dType;

        IF IgnoreCurrentTime THEN
            SET final_o_time = o_time;
            SET final_c_time = c_time;
        ELSEIF dayYear < curDay THEN
            SET final_o_time = NULL;
            SET final_c_time = NULL;
        ELSEIF dayYear = curDay THEN
            IF curTime < o_time THEN
                SET final_o_time = o_time;
                SET final_c_time = c_time;
            ELSEIF curTime < c_time THEN
                SET final_o_time = curTime;
                SET final_c_time = c_time;
            ELSE
                SET final_o_time = NULL;
                SET final_c_time = NULL;
            END IF;
        ELSE
            SET final_o_time = o_time;
            SET final_c_time = c_time;
        END IF;

    END IF;

    SELECT final_o_time, final_c_time;

END$$
DELIMITER ;

MySQL转储:

CREATE TABLE `shop_hours` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `shop_id` int(11) unsigned NOT NULL,
  `type` tinyint(3) NOT NULL DEFAULT '1',
  `day_of_week` int(11) NOT NULL,
  `open_time` time NOT NULL,
  `close_time` time NOT NULL,
  PRIMARY KEY (`id`),
  KEY `shop_id` (`shop_id`),
  CONSTRAINT `shop_hours_ibfk_1` FOREIGN KEY (`shop_id`) REFERENCES `shops` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `shop_hours` (`id`, `shop_id`, `type`, `day_of_week`, `open_time`, `close_time`)
VALUES
    (1,1,1,1,'09:30:00','20:00:00'),
    (2,1,1,2,'09:30:00','20:00:00'),
    (3,1,1,3,'09:30:00','20:00:00'),
    (4,1,1,4,'09:30:00','20:00:00'),
    (5,1,1,5,'09:30:00','20:00:00'),
    (6,1,1,6,'11:00:00','20:00:00'),
    (7,1,1,7,'11:00:00','20:00:00'),
    (8,1,2,1,'11:45:00','12:30:00'),
    (9,1,2,2,'11:45:00','12:30:00'),
    (10,1,2,3,'11:45:00','12:30:00'),
    (11,1,2,4,'11:45:00','12:30:00'),
    (12,1,2,5,'11:45:00','12:30:00'),
    (13,1,2,6,'00:00:00','00:00:00'),
    (14,1,2,7,'00:00:00','00:00:00');

CREATE TABLE `shop_hours_special` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `shop_id` int(11) unsigned NOT NULL,
  `type` tinyint(3) NOT NULL DEFAULT '1',
  `day_of_year` int(11) NOT NULL,
  `open_time` time NOT NULL,
  `close_time` time NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique` (`shop_id`,`type`,`day_of_year`),
  KEY `shop_id` (`shop_id`),
  CONSTRAINT `shop_hours_special_ibfk_1` FOREIGN KEY (`shop_id`) REFERENCES `shops` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `shop_hours_special` (`id`, `shop_id`, `type`, `day_of_year`, `open_time`, `close_time`)
VALUES
    (1,1,1,1,'00:00:00','00:00:00'),
    (2,1,1,92,'00:00:00','00:00:00'),
    (3,1,1,96,'00:00:00','00:00:00'),
    (4,1,1,97,'00:00:00','00:00:00'),
    (5,1,1,99,'00:00:00','00:00:00'),
    (6,1,1,100,'00:00:00','00:00:00'),
    (7,1,1,125,'00:00:00','00:00:00'),
    (8,1,1,138,'00:00:00','00:00:00'),
    (9,1,1,148,'00:00:00','00:00:00'),
    (10,1,1,149,'00:00:00','00:00:00'),
    (11,1,2,1,'00:00:00','00:00:00'),
    (12,1,2,92,'00:00:00','00:00:00'),
    (13,1,2,96,'00:00:00','00:00:00'),
    (14,1,2,97,'00:00:00','00:00:00'),
    (15,1,2,99,'00:00:00','00:00:00'),
    (16,1,2,100,'00:00:00','00:00:00'),
    (17,1,2,125,'00:00:00','00:00:00'),
    (18,1,2,138,'00:00:00','00:00:00'),
    (19,1,2,148,'00:00:00','00:00:00'),
    (20,1,2,149,'00:00:00','00:00:00'),
    (21,1,1,358,'11:00:00','15:00:00'),
    (22,1,2,358,'00:00:00','00:00:00'),
    (23,1,1,359,'00:00:00','00:00:00'),
    (24,1,2,359,'00:00:00','00:00:00'),
    (25,1,1,360,'00:00:00','00:00:00'),
    (26,1,2,360,'00:00:00','00:00:00'),
    (27,1,2,361,'00:00:00','00:00:00'),
    (28,1,1,361,'00:00:00','00:00:00'),
    (29,1,1,366,'11:00:00','14:00:00'),
    (30,1,2,366,'00:00:00','00:00:00'),
    (31,1,2,362,'00:00:00','00:00:00'),
    (32,1,2,363,'00:00:00','00:00:00');
4

0 回答 0