Raja Dhasan
Raja Dhasan

Reputation: 593

Not able to get datetime properly data in Mysql

I have this following in a table

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

namely id's(varchar), fromtime(datetime) and totime(datetime)

I want this id set to be returned when using

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'

But it returns empty set, on the other hand if use this query

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';

It returns the desired results.That is I need to cover the fromtime and totime completely to get my results.Is there a way to get results when my time is like 10:30 - 12:00 ?

Upvotes: 0

Views: 33

Answers (2)

utsav
utsav

Reputation: 622

try this

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';

Upvotes: 0

Giorgos Betsos
Giorgos Betsos

Reputation: 72165

To check for overlapping intervals use these predicates:

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

Demo here

Upvotes: 1

Related Questions