小编典典
考虑以下模式,其中第三个表是提到的年/月帮助程序表。Helper表非常常见,可以自然地在整个代码中重复使用。我将把它留给您以加载大量的日期数据。但是请注意,对于那些想要减少工作量的人来说,每个月的结束日期是如何组合在一起的,同时允许数据库引擎为我们找出leap年。
您在该帮助器表中可能只有一列。但这将需要在某些函数中使用函数调用作为结束日期,这意味着更加缓慢。我们喜欢快速。
架构图
create table workerRecords
( id int auto_increment primary key,
the_date date not null,
staff_no int not null
);
-- truncate workerRecords;
insert workerRecords(the_date,staff_no) values
('2016-06-10',1),
('2016-06-09',1),
('2016-05-09',1),
('2016-04-09',1),
('2016-03-02',2),
('2016-07-02',2);
create table workers
( staff_no int primary key,
full_name varchar(100) not null
);
-- truncate workers;
insert workers(staff_no,full_name) values
(1,'David Higgins'),(2,"Sally O'Riordan");
助手表如下
create table ymHelper
( -- Year Month helper table. Used for left joins to pick up all dates.
-- PK is programmer's choice.
dtBegin date primary key, -- by definition not null
dtEnd date null
);
-- truncate ymHelper;
insert ymHelper (dtBegin,dtEnd) values
('2015-01-01',null),('2015-02-01',null),('2015-03-01',null),('2015-04-01',null),('2015-05-01',null),('2015-06-01',null),('2015-07-01',null),('2015-08-01',null),('2015-09-01',null),('2015-10-01',null),('2015-11-01',null),('2