大家好(节假日前)。

在这种情况下,我在此表中添加了新的请假:

+--------+---------+---------+-------------+----------+--------------------------
|ID_LEAVE|ID_WORKER| FNAME   | LNAME | BEGIN_DATE         | END_DATE            |
+--------+---------+---------+---------+-------------+--------------------+------
| 8      |   10    | MARIO   | NEED  |2019-04-22 07:00:00 |2019-04-23 15:00:00  |
+--------+---------+---------+-------------+----------+--------------------------


但是我知道假期代表着这样的:

mysql - 如何总结无公休假?-LMLPHP

我做了什么?

我已经完成了该查询,该查询总结了按ID_LEAVE分组的所有员工的休假时间

SELECT leave.ID_LEAVE, leave.ID_WORKER, workers.FNAME, workers.LNAME, leave.BEGIN_DATE, leave.END_DATE,
FROM
(SELECT ADDDATE('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) AS date_value
 FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
 (SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
 (SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
 (SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
 (SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) calendar
INNER JOIN leave ON calendar.date_value BETWEEN DATE(leave.BEGIN_DATE) AND DATE(leave.END_DATE)
INNER JOIN workers ON leave.ID_WORKER = workers.ID_WORKER
WHERE NOT WEEKDAY(date_value) IN (5, 6)
GROUP BY ID_LEAVE;


现在,我希望使用该查询来汇总所有员工的休假时间,但没有上图所示的假期。我该怎么办?我应该创建新的“假期”表,然后从该表下载该日期还是在上面的查询中添加日期?

最佳答案

如果您有一个日历表,其中列出了所有的日子(不仅是假期,而且还列出了接下来的X年中的每年的每一天,以及一列有关该天是否为公共假期等的列),则查询看起来像

SELECT l.id_leave, count(*)
FROM calendar c INNER JOIN leave l ON c.calendar_date BETWEEN l.start_date AND l.end_date
WHERE c.day_type NOT IN ('publicholiday', 'weekend')
GROUP BY l.id_leave


您可以添加更多的复杂性,但这是核心。不知道为什么您的叶子从早上7点开始并在晚上10点结束,但是如果您允许小休,您可能需要添加一些逻辑-将开始日期延长到午夜(这样之间可以正常工作),然后在什么情况下使用根据要考虑的日期是开始日期还是结束日期或过渡日期来增加分数

关于mysql - 如何总结无公休假?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55757447/

10-10 19:52