无法在Mysql中正确获取日期时间数据

时间:2016-02-20 06:45:42

标签: mysql database

我在表格中有以下内容

--2,4,1| 2016-02-21 10:22:00 | 2016-02-21 12:22:00 --  

id's(varchar)fromtime(datetime)totime(datetime)

我希望在使用

时返回此ID设置
Select ids 
from Schedule 
where fromtime between '2016/02/21 10:00:00' and '2016/02/21 12:00:00' 
      and totime between '2016/02/21 10:00:00' and '2016/02/21 12:00:00'

但是如果使用此查询

,它会返回空集
Select Participantsids 
from Schedule 
where fromtime between '2016/02/21 10:00:00' and '2016/02/21 13:00:00' 
      and totime between '2016/02/21 10:00:00' and '2016/02/21 13:00:00';

它会返回所需的结果。我需要完全覆盖fromtimetotime以获得我的结果。当我的时间如10:30 - 12时,有办法获得结果:00?

2 个答案:

答案 0 :(得分:1)

要检查重叠间隔,请使用以下谓词:

SELECT ids 
FROM Schedule 
WHERE fromtime <= '2016-02-21 12:00:00' AND totime >= '2016-02-21 10:00:00'

Demo here

答案 1 :(得分:0)

试试这个

Select Participantsids 
from Schedule 
where fromtime between '2016-02-21 10:00:00' and '2016-02-21 13:00:00' 
and totime between '2016-02-21 10:00:00' and '2016-02-21 13:00:00';