主な問題は、検索する時間間隔がないことです。
次のコードは、検索する内部時間間隔を作成します。
DROP VIEW IF EXISTS `time_view` ;
DROP VIEW IF EXISTS `employee_schedule_view` ;
DROP TABLE IF EXISTS `employee_schedule`;
DROP TABLE IF EXISTS `day`;
DROP TABLE IF EXISTS `time`;
CREATE TABLE `time` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`employee_id` int(11) unsigned NOT NULL,
`start_time` time DEFAULT NULL,
`hours` int(11) DEFAULT '0',
`minutes` int(11) DEFAULT '0',
`seconds` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `time` (`id`, `employee_id`, `start_time`, `hours`, `minutes`, `seconds`)
VALUES
(1,1,'22:00:00',5,0,0),
(2,2,'23:00:00',5,0,0),
(3,3,'08:00:00',3,0,0),
(4,4,'13:00:00',4,0,0),
(5,5,'17:00:00',24,0,0),
(6,6,'23:00:00',26,0,0);
/* create a time view that contains interval information */
create or replace view time_view as
select
id as time_id
, start_time
-- calculate the time interval
, SEC_TO_TIME( hours * 3600 + minutes * 60 + seconds ) `interval`
-- calculate the time if we assume time is contiguous
, SEC_TO_TIME( ( hour(start_time) + hours ) * 3600 + ( minute(start_time) + minutes) * 60 + second(start_time) + seconds ) contiguous_time
-- modulate to the number of seconds in a day to get the end time suitable for humans to read
, SEC_TO_TIME((( hour(start_time) + hours ) * 3600 + ( minute(start_time) + minutes) * 60 + second(start_time) + seconds ) % 86400 ) end_time
, floor( (( hour(start_time) + hours ) * 3600 + ( minute(start_time) + minutes) * 60 + second(start_time) + seconds ) / 86400 ) dayOffset
from `time` ;
CREATE TABLE `day` (
`id` int(11) unsigned NOT NULL,
`name` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `day` (`id`, `name`)
VALUES
(0,'Monday'),
(1,'Tuesday'),
(2,'Wednesday'),
(3,'Thursday'),
(4,'Friday'),
(5,'Saturday'),
(6,'Sunday');
CREATE TABLE `employee_schedule` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`employee_id` int(11) unsigned NOT NULL,
`time_id` int(11) unsigned NOT NULL,
`day_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `time_id` (`time_id`),
KEY `day_id` (`day_id`),
CONSTRAINT `employee_schedule_ibfk_2` FOREIGN KEY (`day_id`) REFERENCES `day` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `employee_schedule_ibfk_1` FOREIGN KEY (`time_id`) REFERENCES `time` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `employee_schedule` (`id`, `employee_id`, `time_id`, `day_id`)
VALUES
(1,1,1,0),
(2,1,1,1),
(3,2,1,0),
(4,3,3,0),
(5,4,4,0),
(6,4,1,0),
(7,5,1,5),
(8,4,5,6),
(9,3,6,6),
(10,6,6,1);
/* create an employee view that has interval information */
create or replace view employee_schedule_view as
select schedules.id schedules_id
, employee_id
, day_id as start_day_id
, start_days.name as start_day_name
, `start_time`
, `interval`
, `contiguous_time`
, `end_time`
, `end_days`.`id` as end_day_id
, `end_days`.name as end_day_name
-- some starting point - note the date is incorrect - it is merely here to define a starting point of the interval
, timestamp( concat( date_format( date_add( date( now() ), interval `day_id` day ), '%Y-%m-%d' ) , ' ' , start_time )) `internal_start`
-- some ending point - note the date is incorrect - it is merel here to define an ending point of the interval
, addtime( timestamp( concat( date_format( date_add( date( now() ), interval `day_id` day ), '%Y-%m-%d' ) , ' ' , `start_time` )) , `interval` ) `internal_finish`
from `employee_schedule` `schedules`
inner join `day` `start_days`
on `schedules`.`day_id` = `start_days`.id
inner join `time_view` `times`
on `schedules`.`time_id` = `times`.`time_id`
inner join `day` `end_days`
-- use mod to allow for instance when the start day is 'after' the end day -- think about sunday
on `end_days`.`id` = ( `start_days`.id + `times`.dayOffset ) % 7
間隔がサポートされると、次のSQLを簡単に実行できます...そして理解できます。
-- example
-- find who is working at 11:00 on a Wednesday
set @dayID := 2 ;
set @time := '11:00:00' ;
select employee_schedule_view.schedules_id
, employee_schedule_view.employee_id
, employee_schedule_view.start_day_name
, employee_schedule_view.start_time
, employee_schedule_view.interval
, employee_schedule_view.end_time
, employee_schedule_view.end_day_name
from `employee_schedule_view`
inner join
(
select concat( date_format( date_add( date(now()), interval @dayID day ), '%Y-%m-%d' ), ' ', @time) internal_check
) internal_check
where
internal_check.internal_check >= internal_start
and internal_check.internal_check <= internal_finish
;
-- find who is working at 23:00 on a Monday
set @dayID := 0 ;
set @time := '23:00:00' ;
select employee_schedule_view.schedules_id
, employee_schedule_view.employee_id
, employee_schedule_view.start_day_name
, employee_schedule_view.start_time
, employee_schedule_view.interval
, employee_schedule_view.end_time
, employee_schedule_view.end_day_name
from `employee_schedule_view`
inner join
(
select concat( date_format( date_add( date(now()), interval @dayID day ), '%Y-%m-%d' ), ' ', @time) internal_check
) internal_check
where
internal_check.internal_check >= internal_start
and internal_check.internal_check <= internal_finish
;
-- example results
1 1 Monday 22:00:00 05:00:00 03:00:00 Tuesday
3 2 Monday 22:00:00 05:00:00 03:00:00 Tuesday
6 4 Monday 22:00:00 05:00:00 03:00:00 Tuesday