MySQLプロシージャを使用して、2つの異なるテーブルに基づいてショップの開店時間と閉店時間を取得しています。通常の「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');