如何总结最小begin_date和最大end_date之间的时间差-MySQL

时间:2019-03-18 19:16:59

标签: mysql datetime max min difference

具有一个包含1条记录的“订单”表:

+--------+---------+---------+------------+
|ID_ORDER  |  DESC_ORDER  | NUMBER_ORDER  |
+----------+---------+--------------------+
| 19       |  TEST        |  TEST         |
+----------+---------+--------------------+

和一个带有2条记录的“工人”:

+--------+---------+---------+--
|ID_WORKER |  FNAME  | LNAME   |
+----------+---------+----------
| 25       |  ADAM   |  GAX    |
+----------+---------+----------
| 26       |  ANDREW |  WORM   |
+----------+---------+----------

然后我创建了“ status_order”:

+--------+---------+---------+---------+-------------+--------+----------+------------+
| Id_status|ID_WORKER| ID_ORDER| BEGIN_DATE          | END_DATE          | ORDER_DONE |
+----------+---------+---------+----------+------------+---------+--------------------+
| 30       |   26    |    19   |2019-03-18 06:40:21  |2019-03-18 15:22:32|  NO        |
+----------+---------+---------+------------+---------+-------------------+-----------+ 
| 31       |   25    |    19   |2019-03-18 06:51:23  |2019-03-18 15:11:10|  NO        |
+----------+---------+---------+------------+---------+-------------------+-----------+ 
| 32       |   26    |    19   |2019-03-20 06:23:32  |2019-03-20 15:33:11|  NO        |
+----------+---------+---------+------------+---------+-------------------+-----------+ 
| 33       |   25    |    19   |2019-03-20 06:44:42  |2019-03-20 15:16:22|  NO        |
+----------+---------+---------+------------+---------+-------------------+-----------+ 
| 34       |   26    |    19   |2019-03-22 06:44:32  |2019-03-22 11:44:54|  YES       |
+----------+---------+---------+------------+---------+-------------------+-----------+ 
| 35       |   25    |    19   |2019-03-22 06:59:22  |2019-03-22 12:33:33|  YES       |
+----------+---------+---------+------------+---------+-------------------+-----------+ 

我需要一个MYSQL查询,该查询将正确总结min(begin_date)和max(end_date)之间的日期时间。我在下面尝试了该查询:

SELECT
  orders.NUMBER_ORDER, 
  orders.DESC_ORDER,
  SEC_TO_TIME(SUM(TIME_TO_SEC(status_order.END_DATE) -
    TIME_TO_SEC(status_order.BEGIN_DATE))) AS 'TOTAL TIME' 
FROM status_order 
INNER JOIN orders ON orders.ID_ORDER = status_order.ID_ORDER 
WHERE orders.NUMBER_ORDER LIKE 'TEST'

得到结果:

+--------+---------+---------+------------+
|DESC_ORDER  |  NUMBER_ORDER | TOTAL TIME |
+----------+---------+--------------------+
| TEST       |  TEST         |  45:17:50  |
+----------+---------+--------------------+

那总共的时间对我来说太多了。我已经通过以下mysql命令进行了检查:

SELECT workers.FNAME, workers.LNAME, orders.NUMBER_ORDER, 
  orders.DESC_ORDER,
  SEC_TO_TIME(SUM(TIME_TO_SEC(status_order.END_DATE) -
    TIME_TO_SEC(status_order.BEGIN_DATE))) AS 'TOTAL TIME' 
FROM status_order
INNER JOIN orders ON orders.ID_ORDER = status_order.ID_ORDER 
INNER JOIN workers ON workers.ID_WORKER = status_order.ID_WORKER 
WHERE orders.NUMBER_ORDER LIKE 'TEST' 
GROUP BY workers.ID_WORKER

然后我得到结果:

+--------+---------+---------+-------+------------+------------+
|  FNAME  | LNAME   |  NUMBER_ORDER  | DESC_ORDER | TOTAL TIME |
+----------+---------+---------------+------------+------------+
|  ADAM   |  GAX    | TEST           | TEST       | 22:25:38   |
+----------+---------+---------------+------------+------------+
|  ANDREW |  WORM   | TEST           | TEST       | 22:52:12   |
+----------+---------+---------------+------------+------------+

我认为此订单的时差应为21-22小时左右,但我想正确地总结一下时差,例如“ TOTAL TIME”:

+--------+--
|TOTAL TIME|
+--------+--
|22:52:12  |
+--------+--

对不起,我写了那个任务/问题,但对我来说很困难,我不是MYSQL专家。有任何想法吗?我应该写什么样的查询。非常感谢您的帮助!

0 个答案:

没有答案