Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
My table
----------------------------------------------------------
Reservation ID | StartDateTime       | EndDateTime
00001          | 2013-06-01 01:00:00 | 2013-06-01 03:00:00
00002          | 2013-06-01 02:00:00 | 2013-06-01 05:00:00
00003          | 2013-06-01 03:00:00 | 2013-06-01 06:00:00
00004          | 2013-06-04 06:00:00 | 2013-06-01 07:00:00
00004          | 2013-06-04 09:00:00 | 2013-06-01 11:00:00
-
-
-

I try to find record from 2013-06-01 02:00:00 to 2013-06-01 03:00:00. With the following
SQL
SELECT *
FROM reservation
WHERE((startDateTime BETWEEN '2013-06-01 02:00:00' AND '2013-06-01 03:00:00') OR
(endDateTime BETWEEN '2013-06-01 02:00:00' AND '2013-06-01 03:00:00') OR
('2013-06-01 02:00:00' BETWEEN startDateTime AND endDateTime) OR
('2013-06-01 03:00:00' BETWEEN startDateTime AND endDateTime));


The result
----------------------------------------------------------
Reservation ID | StartDateTime       | EndDateTime
00001          | 2013-06-01 01:00:00 | 2013-06-01 03:00:00
00002          | 2013-06-01 02:00:00 | 2013-06-01 05:00:00
00003          | 2013-06-01 03:00:00 | 2013-06-01 06:00:00 <--- wrong


I don't want the 03:00:00 because it's out of my time range. How to omit the 03:00:00. It's hard to explain. I want reservation ID of 02:00:00 to 03:00:00 but it gave me extra one. Although the result is correct and work as function but ....sigh. idk.
Posted
Updated 4-Jun-13 7:22am
v2
Comments
Mohibur Rashid 5-Jun-13 5:18am    
id 4 came twice, ignorable as typo, both 4 start date is 2013-06-04 and end on 2013-06-01
Valentine1993 6-Jun-13 3:06am    
ya typo. Srry

It's simple.

SQL
SELECT *
FROM reservation
WHERE (startDateTime >='2013-06-01 02:00:00') AND (endDateTime <='2013-06-01 03:00:00'

Above query returns 0 records.

SQL
SELECT *
FROM reservation
WHERE (startDateTime BETWEEN '2013-06-01 02:00:00'  AND '2013-06-01 03:00:00') AND (endDateTime BETWEEN '2013-06-01 02:00:00'  AND '2013-06-01 03:00:00')

Above query returns 0 records.Test it with OR operator between two conditions:

SQL
SELECT *
FROM reservation
WHERE (startDateTime BETWEEN '2013-06-01 02:00:00'  AND '2013-06-01 03:00:00') OR (endDateTime BETWEEN '2013-06-01 02:00:00'  AND '2013-06-01 03:00:00')

What's the difference?

SQL
SELECT *
FROM reservation
WHERE (startDateTime BETWEEN '2013-06-01 02:00:00'  AND '2013-06-01 03:00:00')

Above query returns ID 00002.

SQL
SELECT *
FROM reservation
WHERE (endDateTime BETWEEN '2013-06-01 02:00:00'  AND '2013-06-01 03:00:00')

Above query returns ID 00001.

Please, read about MySQL logical operators[^].
 
Share this answer
 
v3
Comments
Valentine1993 4-Jun-13 22:44pm    
SELECT *
FROM reservation
WHERE (startDateTime BETWEEN '2013-06-01 02:00:00' AND '2013-06-01 03:00:00') OR (endDateTime BETWEEN '2013-06-01 02:00:00' AND '2013-06-01 03:00:00')

Return 3 Query. 00001, 00002, 00003
Valentine1993 4-Jun-13 22:48pm    
SELECT *
FROM reservation
WHERE (startDateTime BETWEEN '2013-06-01 02:00:00' AND '2013-06-01 03:00:00')
Return 2 Query. 00002, 00003. Although the returned query is correct. But I don't want the 3:00:00 because 03:00:00 to 06:00:00 is not part of 02:00:00 to 03:00:00. 00003 just touched the range of 02:00:00 to 03:00:00
Valentine1993 4-Jun-13 23:07pm    
The proper question is "Returning time interval intersection"
Maciej Los 5-Jun-13 1:49am    
Try this:
SELECT *
FROM reservation
WHERE (startDateTime BETWEEN '2013-06-01 02:00:00' AND '2013-06-01 02:59:59')
The ultimate answer.
SQL
Select *
From reservation
Where '2013-06-01 02:00:00' < endDateTime AND '2013-06-01 03:00:00' > startDateTime

Thank you everyone!!!
 
Share this answer
 
Comments
Maciej Los 5-Jun-13 4:21am    
GOod job! I would suggest you to mark all answers as "solved" (grenn button) - foramlly.
Have a 5 from me!
Valentine1993 5-Jun-13 5:18am    
oh yeah. Hey mark mine as solved? i'm newbie
Maciej Los 5-Jun-13 6:14am    
Both ;) Your and mine...
Valentine1993 6-Jun-13 3:05am    
(:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900