PHP 按天分组时间事件
PHP group time events by day
我已经研究这个主题几个小时了,但未能设计出适合我的问题的解决方案。这个答案最接近我的问题,但我不需要每天的总数。让我解释一下:
我有一个正在 php 中构建的程序,它是兼职员工的打卡系统,到目前为止我遇到的最大问题是最后的时间显示。我有单独的 table 存储每周总小时数,以及每个打卡或打卡的日志。我想在每周结束时将两者显示在一个 table 中。
这意味着我需要一种按天对所有日志事件进行分组的方法(平均每天 4 个,时钟 in/out 午餐前后),因此我可以将其全部显示在行中。我希望最后的 table 具有以下顺序的信息:
天,在(时间),在(时间),在(时间),在(时间),日期
我 运行 遇到的问题是当我 'GROUP BY date_updated' 它会自动尝试将所有数字加在一起。
编辑:这里是 times
table(保留每个动作时间):
CREATE TABLE `times` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` text,
`status` text,
`action_time` time DEFAULT NULL,
`action_date` datetime DEFAULT NULL,
`next` text, // Next status (In or Out) used for button
`clockIn` time DEFAULT NULL,
`clockOut` time DEFAULT NULL,
`department` text,
`tdate` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1234 DEFAULT CHARSET=utf8
这里是 hours
table(保留每天的时间):
CREATE TABLE `hours` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`day` text,
`user_id` text,
`calc_time` text,
`date_time` datetime DEFAULT NULL,
`hours` float DEFAULT NULL,
`weekly_hours` float DEFAULT NULL,
`department` text,
`date_updated` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=99 DEFAULT CHARSET=utf8
我想创建一个类似于此的 table:
Link to a timesheet image since I can't post images (Need 10 Reputation)
Link to a screenshot of some example output from those tables
这是为了帮助您入门。 FK=外键约束。数据库强制执行参照完整性。不错的读物。
架构
create table actionCodes
( -- Allowable action codes in system, will be enforced thru FK
action char(5) primary key,
description varchar(50) not null
);
insert actionCodes (action,description) values ('CI','Clock In'),('CO','Clock Out'),('Fired','Clocked Out and Fired');
-- drop table rtd;
create table rtd
( -- Raw Time Data
id int AUTO_INCREMENT primary key,
userId int not null, -- FK to user table, not shown
# for this example, use a char to make the code more readable
action char(5) not null, -- code, FK to action code table, int is the way I personally would go
actionDt datetime not null,
CONSTRAINT fk_rtd_acodes FOREIGN KEY (action) REFERENCES actionCodes(action)
);
insert rtd (userId,action,actionDt) values (1,'CI','2015-09-02 10:23:03');
insert rtd (userId,action,actionDt) values (1,'CO','2015-09-02 12:01:03');
insert rtd (userId,action,actionDt) values (1,'CI','2015-09-02 10:03:23');
insert rtd (userId,action,actionDt) values (1,'CO','2015-09-02 10:40:00');
insert rtd (userId,action,actionDt) values (1,'CI','2015-09-02 10:50:23');
insert rtd (userId,action,actionDt) values (1,'CO','2015-09-02 16:00:03');
insert rtd (userId,action,actionDt) values (1,'XXX','2015-09-03 09:00:03'); -- bad code, error 1452
没有理由将 'next code' 与时间和日期放在不同的列中。即使与手头的代码无关,也可以在两列中打卡和打卡。它只是一个代码和一个日期时间。部门永远不会在这个 table 中(这是非常不规范的)。
这个问题现在立即属于 mysql 变量的处理(一种几乎像游标的机制)。至少在我看来。我将不得不稍后再回来,因为它们可能非常耗时。像这样 one 我昨晚做的。每个 可以 都有自己独特的复杂性,而您的复杂性的形式是 如果他们不这样做会怎样 Clock Out
.
也许其他人可以在这里接球。
我知道想要分而治之的趋势,并汇总到另一个 table。相信我,我们都去过那里。但是这个数据可以一网打尽table.
事实上,您有 user_id 和状态作为文本,以及您所做的专栏,我认为现在不是 mysql 变量的时候。我这么说并不是无礼。我会点击教程。 Whosebug 不是那种网站。
我已经研究这个主题几个小时了,但未能设计出适合我的问题的解决方案。这个答案最接近我的问题,但我不需要每天的总数。让我解释一下:
我有一个正在 php 中构建的程序,它是兼职员工的打卡系统,到目前为止我遇到的最大问题是最后的时间显示。我有单独的 table 存储每周总小时数,以及每个打卡或打卡的日志。我想在每周结束时将两者显示在一个 table 中。
这意味着我需要一种按天对所有日志事件进行分组的方法(平均每天 4 个,时钟 in/out 午餐前后),因此我可以将其全部显示在行中。我希望最后的 table 具有以下顺序的信息:
天,在(时间),在(时间),在(时间),在(时间),日期
我 运行 遇到的问题是当我 'GROUP BY date_updated' 它会自动尝试将所有数字加在一起。
编辑:这里是 times
table(保留每个动作时间):
CREATE TABLE `times` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` text,
`status` text,
`action_time` time DEFAULT NULL,
`action_date` datetime DEFAULT NULL,
`next` text, // Next status (In or Out) used for button
`clockIn` time DEFAULT NULL,
`clockOut` time DEFAULT NULL,
`department` text,
`tdate` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1234 DEFAULT CHARSET=utf8
这里是 hours
table(保留每天的时间):
CREATE TABLE `hours` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`day` text,
`user_id` text,
`calc_time` text,
`date_time` datetime DEFAULT NULL,
`hours` float DEFAULT NULL,
`weekly_hours` float DEFAULT NULL,
`department` text,
`date_updated` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=99 DEFAULT CHARSET=utf8
我想创建一个类似于此的 table:
Link to a timesheet image since I can't post images (Need 10 Reputation)
Link to a screenshot of some example output from those tables
这是为了帮助您入门。 FK=外键约束。数据库强制执行参照完整性。不错的读物。
架构
create table actionCodes
( -- Allowable action codes in system, will be enforced thru FK
action char(5) primary key,
description varchar(50) not null
);
insert actionCodes (action,description) values ('CI','Clock In'),('CO','Clock Out'),('Fired','Clocked Out and Fired');
-- drop table rtd;
create table rtd
( -- Raw Time Data
id int AUTO_INCREMENT primary key,
userId int not null, -- FK to user table, not shown
# for this example, use a char to make the code more readable
action char(5) not null, -- code, FK to action code table, int is the way I personally would go
actionDt datetime not null,
CONSTRAINT fk_rtd_acodes FOREIGN KEY (action) REFERENCES actionCodes(action)
);
insert rtd (userId,action,actionDt) values (1,'CI','2015-09-02 10:23:03');
insert rtd (userId,action,actionDt) values (1,'CO','2015-09-02 12:01:03');
insert rtd (userId,action,actionDt) values (1,'CI','2015-09-02 10:03:23');
insert rtd (userId,action,actionDt) values (1,'CO','2015-09-02 10:40:00');
insert rtd (userId,action,actionDt) values (1,'CI','2015-09-02 10:50:23');
insert rtd (userId,action,actionDt) values (1,'CO','2015-09-02 16:00:03');
insert rtd (userId,action,actionDt) values (1,'XXX','2015-09-03 09:00:03'); -- bad code, error 1452
没有理由将 'next code' 与时间和日期放在不同的列中。即使与手头的代码无关,也可以在两列中打卡和打卡。它只是一个代码和一个日期时间。部门永远不会在这个 table 中(这是非常不规范的)。
这个问题现在立即属于 mysql 变量的处理(一种几乎像游标的机制)。至少在我看来。我将不得不稍后再回来,因为它们可能非常耗时。像这样 one 我昨晚做的。每个 可以 都有自己独特的复杂性,而您的复杂性的形式是 如果他们不这样做会怎样 Clock Out
.
也许其他人可以在这里接球。
我知道想要分而治之的趋势,并汇总到另一个 table。相信我,我们都去过那里。但是这个数据可以一网打尽table.
事实上,您有 user_id 和状态作为文本,以及您所做的专栏,我认为现在不是 mysql 变量的时候。我这么说并不是无礼。我会点击教程。 Whosebug 不是那种网站。