简体   繁体   English

MySQL 14天重置查询

[英]MySQL 14 day reset query

I'm building a basic system that selects 2 staff members at random to support the business for half of the day. 我正在建立一个基本系统,该系统随机选择2名员工来支持一天的业务。 I'm really struggling with the database and how I would actually go about completing this task as they've listed certain rules which are making it quite tricky. 我真的在数据库上苦苦挣扎,实际上我将如何完成该任务,因为他们列出了某些使其变得非常棘手的规则。

Business Rules: 商业规则:

  • An engineer can do at most one half-day shift in a day. 工程师一天最多只能轮班半天。

  • An engineer cannot have half day shifts on consecutive days. 工程师不能连续两天轮班。

  • Each engineer should have completed one whole day of support in any 2 week period. 每个工程师应在任何两周的时间内完成一整天的支持。

I'm a real beginner with this, but so far this is how I think of the problem: 我是一个真正的初学者,但是到目前为止,这是我对问题的看法:

Two Tables: 两张桌子:

EMPLOYEE: 雇员:

employee_id (pk)
first_name
last_name

SHIFTS: 转变:

shift_id (pk)
employee_id (fk)
shift_hours

SAMPLE DATA: 样本数据:

 1. shift_id = 1, employee_id = 1, shift_hours = 4
 2. shift_id = 2, employee_id = 2, shift_hours = 6

 1. employee_id = 1, first_name = steve, second_name = jones
 2. employee_id = 2, first_name = rob, second_name = helen

My question is as follows: Based on the data above, what can I query to ensure that I am picking only users from the database that have only worked LESS than 8 hours in the past 14 days (How can I go about this 14 day issue, can there be some kind of "reset" 24 hours after somebody hits 8 the 8 hour mark, is that possible?) 我的问题如下:根据上面的数据,我可以查询什么以确保仅从数据库中挑选过去14天内工作少于8小时的用户(如何处理这14天的问题) ,有人打8 8小时标记后24小时会发生某种“重置”吗?)

Hopefully my question is clear, I do apologise as I am a bit of a beginner. 希望我的问题很清楚,我还是个初学者,对此我深表歉意。

Thanks in advance 提前致谢

(This is different from my previous question, please see the bottom, I have merely just copied the explanation of the problem) (这与我之前的问题有所不同,请看底部,我只是复制了问题的解释)

First thing i would not necessarily do shift_hours i would do like started_at and ended_at and make them datetime columns. 第一件事,我不一定会做shift_hours我会做这样started_atended_at ,使他们datetime列。 This way you can do date math in your query to derive the amount of time worked, and you can select between specific dates. 这样,您可以在查询中进行日期数学运算以得出工作时间,并且可以在特定日期之间进行选择。

With that as the premise maybe the tables look like so: 以此为前提,表格可能如下所示:

CREATE TABLE `employee` (
  id INTEGER AUTO_INCREMENT NOT NULL,
  first_name VARCHAR(32) NOT NULL,
  last_name VARCHAR(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB;

CREATE TABLE `shift` (
  id INTEGER AUTO_INCREMENT NOT NULL,
  started_at DATETIME NOT NULL,
  ended_at DATETIME NOT NULL,
  employee_id INTEGER,
  PRIMARY KEY (`id`),
  KEY `shift_employee_id` (`employee_id`),
  FOREIGN KEY `shift_employee_id_fk_employee_id` (`employee_id`)
    REFERENCES `employee` (`id`)
    ON DELETE CASCADE
) ENGINE=INNODB;

So now we could query like: 所以现在我们可以像这样查询:

SELECT e.first_name, e.last_name, SUM(ABS(TIMESTAMPDIFF(HOUR, s.started_at, s.ended_at))) as shift_hours
FROM employee e, shift s
WHERE e.id = s.employee_id
AND s.started_at BETWEEN '2018-01-01' AND '2018-01-15'
GROUP BY e.id
HAVING shift_hours < 8;

This would give us only employees who have logged less than 8 hours between the given dates we supply. 这将只给我们在给定日期之间记录少于8小时的员工。

SQL fiddle: http://sqlfiddle.com/#!9/280d88/6 (note, i didnt want to generate enough data to use your 14 day rule, but you should get the idea). SQL小提琴: http ://sqlfiddle.com/#!9 / 280d88 / 6 (请注意,我不想生成足够的数据来使用您的14天规则,但您应该明白这一点)。

The other rules are ones you will want to apply in the logic of your code when assigning shifts. 其他规则是您在分配班次时希望在代码逻辑中应用的规则。 Though you could probably do it in a stored procedure. 尽管您可能可以在存储过程中做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM