В базе данных mysql я изменил таблицу «выхода»:
+--------+---------+---------+-------------+---------+--------------------------
|ID_LEAVE|ID_WORKER| FNAME | LNAME | BEGIN_DATE | END_DATE |
+--------+---------+---------+---------+------------+--------------------+------
| 1 | 4 | LEON | SPEED |2019-03-22 07:00:00 |2019-03-23 15:00:00 |
+--------+---------+---------+-------------+---------+--------------------------
«Таблица рабочих»
+----------+---------+---------+
|ID_WORKER | FNAME | LNAME |
+----------+---------+----------
| 4 | LEON | SPEED |
| 6 | JACK | FAR |
+----------+---------+---------+
Таблица «Заказы»:
+----------+--------------+---------------+
|ID_ORDER | DESC_ORDER | NUMBER_ORDER |
+----------+--------------+---------------+
| 20 | TEST | TEST |
+----------+--------------+---------------+
»Таблица "Order_status":
+----------+---------+---------+---------------------+-------------------+------------+
| Id_status|ID_WORKER| ID_ORDER| BEGIN_DATE | END_DATE | ORDER_DONE |
+----------+---------+---------+----------+------------+---------+--------------------+
| 30 | 4 | 20 |2019-03-18 06:50:35 |2019-03-18 15:21:32| NO |
| 31 | 4 | 20 |2019-03-20 06:44:12 |2019-03-20 15:11:23| NO |
| 32 | 4 | 20 |2019-03-22 06:50:20 |2019-03-22 12:22:33| YES |
| 33 | 6 | 20 |2019-03-18 06:45:11 |2019-03-18 15:14:45| NO |
| 34 | 6 | 20 |2019-03-20 06:50:22 |2019-03-20 15:10:32| NO |
| 35 | 6 | 20 |2019-03-22 06:54:11 |2019-03-22 11:23:45| YES |
+----------+---------+---------+------------+---------+-------------------+-----------+
Что я сделал:
Я могу суммировать "Время заказа" работников друг друга (в таблице order_status) в заказе, в том числе и с помощью sumarizing.время "из таблицы Leave и обобщающих" время заказа "+" время выхода ".Я правильно выбрал рабочие заказы (LNAME, FNAME) (DESC_ORDER и NUMBER_ORDER) и "ВСЕГО ВРЕМЕНИ" для заказов друг друга.Я написал команду mysql ниже:
SELECT workers.FNAME,
workers.LNAME,
order_statusAgg.NUMBER_ORDER,
order_statusAgg.DESC_ORDER,
SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'ORDER TIME',
IFNULL(SEC_TO_TIME(SUM(leaveAgg.ltime)),'00:00:00') AS 'LEAVE TIME',
ADDTIME(SEC_TO_TIME(SUM(order_statusAgg.stime)), IFNULL(SEC_TO_TIME(SUM(leaveAgg.ltime)), '00:00:00')) AS 'TOTAL TIME'
FROM workers
LEFT JOIN (SELECT leave.ID_WORKER, SUM((datediff(leave.BEGIN_DATE, leave.END_DATE) + 1) * (time_to_sec(time(leave.END_DATE)) - time_to_sec(time(leave.BEGIN_DATE)))) AS ltime FROM leave
LEFT JOIN
(SELECT ID_WORKER, MIN(BEGIN_DATE) AS 'MIN_BEGIN_DATE', MAX(END_DATE) AS 'MAX_END_DATE'
FROM order_status GROUP BY ID_WORKER) ordstat ON
leave.ID_WORKER = ordstat.ID_WORKER
WHERE leave.END_DATE <= MAX_END_DATE AND leave.BEGIN_DATE >= MIN_BEGIN_DATE GROUP BY leave.ID_WORKER) leaveAgg
ON leaveAgg.ID_WORKER = workers.ID_WORKER
LEFT JOIN (
SELECT order_status.ID_WORKER, orders.NUMBER_ORDER, orders.DESC_ORDER,
SUM((TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))) AS stime
FROM order_status INNER JOIN orders
ON orders.ID_ORDER = order_status.ID_ORDER
GROUP BY order_status.ID_WORKER) order_statusAgg
ON workers.ID_WORKER = order_statusAgg.ID_WORKER
WHERE order_statusAgg.NUMBER_ORDER LIKE 'TEST'
GROUP BY workers.ID_WORKER;
Но после этого что-то пошло не так:
+---------+---------+---------------+------------+------------+--------------+-----------+
| FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | ORDER TIME | LEAVE_TIME | TOTAL TIME|
+---------+---------+---------------+------------+------------+--------------+-----------+
| LEON | SPEED | TEST | TEST | 22:30:21 | 00:00:00 | 22:30:21 |
+---------+---------+---------------+------------+------------+--------------+-----------+
| JACK | FAR | TEST | TEST | 21:19:18 | 00:00:00 | 21:19:18 |
+---------+---------+---------------+------------+------------+--------------+-----------+
, почему «Leave_time» Leon Speed установлен на 00:00:00, несмотря на то, что онбыло время отпуска в: с 2019-03-22 07:00:00 до 2019-03-23 15: 00: 00.
Для меня должно быть что-то другое:
+---------+---------+---------------+------------+------------+--------------+-----------+
| FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | ORDER TIME | LEAVE_TIME | TOTAL TIME|
+---------+---------+---------------+------------+------------+--------------+-----------+
| LEON | SPEED | TEST | TEST | 22:30:21 | 08:00:00 | 30:30:21 |
+---------+---------+---------------+------------+------------+--------------+-----------+
| JACK | FAR | TEST | TEST | 21:19:18 | 00:00:00 | 21:19:18 |
+---------+---------+---------------+------------+------------+--------------+-----------+
потому что заказ закончился 22-03-2019 (несмотря на то, что у него был отпуск с 22-03-2019 по 23-03-2019).Это из-за строк кода ниже?
LEFT JOIN (SELECT leave.ID_WORKER, SUM((datediff(leave.BEGIN_DATE, leave.END_DATE) + 1) * (time_to_sec(time(leave.END_DATE)) - time_to_sec(time(leave.BEGIN_DATE)))) AS ltime FROM leave
LEFT JOIN
(SELECT ID_WORKER, MIN(BEGIN_DATE) AS 'MIN_BEGIN_DATE', MAX(END_DATE) AS 'MAX_END_DATE'
FROM order_status GROUP BY ID_WORKER) ordstat ON
leave.ID_WORKER = ordstat.ID_WORKER
WHERE leave.END_DATE <= MAX_END_DATE AND leave.BEGIN_DATE >= MIN_BEGIN_DATE GROUP BY leave.ID_WORKER) leaveAgg
ON leaveAgg.ID_WORKER = workers.ID_WORKER
Мне нужна помощь, чтобы разобраться с этим.Я не ленивый парень, но я не гуру Mysql (потому что я тренируюсь 3 недели).Может кто-нибудь дать совет или идею, что мне делать?Заранее благодарю за любой совет.