开发者

MySQL check between date and time 2 colums

Have a problem to check if an entry exist between two DATETIME columns. Creating a booking system for massages, i list times 08:00, 08:15 08:30, 08:45 and so on. I want to check if there is a slot for example from 13:00:00 and two hours forward. (This treatment is 120 min, got other treatments with 90 min duration etc).

The table i开发者_如何学Pythons like this

|id|event|from_date|to_date|

1 | massage | 2011-08-15 14:00:00 | 2011-08-15 16:00:00

the query

$times_from_date = '2011-08-15 13:00:00';
$times_to_date = '2011-08-15 16:00:00';

SELECT * FROM `wp_byforssell_booking` WHERE '". $times_from_date ."' BETWEEN from_date AND to_date OR '". $times_from_date ."' BETWEEN from_date AND to_date OR '". $from_to_date

It dosent work. Any ideas?

EDIT

Lets say i got the appointment in my database as above.

I want to Query all the appointments between 2011-08-15 13:45 and 2011-08-15 15:30.

That is my problem. Hope that is a better explanation to the problem.

EDIT

My table looks like this now

id|event|from_date|to_date|

1 | massage(60min) | 2011-08-15 13:30:00 | 2011-08-15 15:30:00

2 | massage(60min) | 2011-08-15 09:00:00 | 2011-08-15 10:00:00

And my Query: Select time från 09:30 and 11:30, and it dose not produce row#2.

SELECT * FROM wp_byforssell_booking WHERE((('2011-08-15 09:30:00' <= from_date)AND('2011-08-15 11:30:00' > from_date)) OR (('2011-08-15 09:30:00' >= from_date)AND('2011-08-15 11:30:00' < to_date)))


You have a typo. You declared $times_to_date but your query uses $from_to_date.

...BETWEEN from_date AND to_date OR '". $times_from_date ."' BETWEEN from_date AND to_date OR '". $from_to_date
    ---------------------------------------------------------------------------------------------^^^^^^^^^^^^^^

Should be:

...BETWEEN from_date AND to_date OR '". $times_from_date ."' BETWEEN from_date AND to_date OR '". $times_to_date


In addition to what Michael points out, you're also doing the same WHERE clause twice:

... WHERE '". $times_from_date ."' BETWEEN from_date AND to_date OR '". $times_from_date ."' BETWEEN from_date AND to_date ...

Isn't one of them supposed to check $times_to_date?


If I understand correctly, you want to list all buckets where there's enough time difference between a start time and an end time (in this case $times_from_date and $times_to_date) and this will do it, I think:

SELECT * FROM wp_byforssell_booking 
WHERE exists 
      (select 1 from wp_byforssell_booking where   
      timestampdiff(HOURS,from_date,to_date)>=
      timestapdiff(HOURS,$times_from_date,$times_to_date)


To get records which do overlap with datetime ranges given with $times_from_date and $times_to_date you use query like

SELECT * FROM wp_byforssell_booking WHERE
(
(($times_from_date <= from_date)AND($times_to_date > from_date))
OR
(($times_from_date >= from_date)AND($times_from_date < to_date))
)

You might have to use < instead of <= and > instead of >= - that depends on your logic, ie does two events overlap if one ends at 2011-08-15 14:00:00 and other starts at 2011-08-15 14:00:00 (IOW is it required to start 2011-08-15 14:00:01 in order to not overlap).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜