如何在Begin_date和End_date之间选择范围-MySQL

时间:2019-04-18 08:12:55

标签: mysql datetime range

在这种情况下,我有“离开”表:

+--------+---------+---------+-------------+---------+-----------+
|ID_LEAVE|ID_WORKER| BEGIN_DATE          | END_DATE              |
+--------+---------+---------+---------+------------+------------+
| 4      |   26    |2019-03-19 07:00:00  |2019-03-22 15:00:00    |  
+--------+---------+---------+----------------------+------------+

要编写mysql查询,该查询选择Begin Date和End date之间的所有范围,然后进行汇总。当我这样写的时候:

SELECT *, 
time_format(SUM((datediff(END_DATE, BEGIN_DATE) + 1) * (time(END_DATE) - time(BEGIN_DATE))), '%H:%i:%s') AS 'LEAVE TIME' 
FROM leave 
WHERE (DATE(BEGIN_DATE) >= '2019-03-19' AND DATE(END_DATE) <='2019-03-22') 
GROUP BY ID_LEAVE;

然后显示如下:

    +--------+---------+---------+-------------+---------+------------------------+
    |ID_LEAVE|ID_WORKER| BEGIN_DATE          | END_DATE              | LEAVE_TIME |
    +--------+---------+---------+---------+------------+--------------------+-----
    | 4      |   26    |2019-03-19 07:00:00  |2019-03-22 15:00:00    |  32:00:00  |
    +--------+---------+---------+----------------------+------------+------------- 

但是当我这样做的时候:

SELECT *, 
time_format(SUM((datediff(END_DATE, BEGIN_DATE) + 1) * (time(END_DATE) - time(BEGIN_DATE))), '%H:%i:%s') AS 'LEAVE TIME' 
FROM leave 
WHERE (DATE(BEGIN_DATE) >= '2019-03-19' AND DATE(END_DATE) <='2019-03-21') 
GROUP BY ID_LEAVE;

什么也没显示。

我有一个问题:是否可以全部显示(开始日期和结束日期)?如果可以,请解释如何写?感谢您的回答。

0 个答案:

没有答案