I have a table in mysql like this:
+------------+------------+------------+------------+
| date | user_id | start_hour | end_hour |
+------------+------------+------------+------------+
| 2010-12-15 | 20 | 08:00:00 | 08:15:00 |
| 2010-12-15 | 20 | 14:00:00 | 14:30:00 |
| 2010-12-15 | 20 | 17:00:00 | 17:45:00 |
+------------+------------+------------+------------+
and I try to extract the time range of the time of users I found and example here, but I can't make that work on hours
I tried the query:
$sql="
SELECT a.end_hour AS 'Available From', Min(b.start_hour) AS 'To'
FROM (
SELECT 0 as date, '08:00:00' as start_hour,'08:00:00' as end_hour
UNION SELECT date, start_hour, end_hour FROM table
)
AS a JOIN
( SELECT date, start_hour, end_hour FROM table
UNION SELECT 0, '21:00:00' as start_hour, '22:00:00' as end_hour
) AS b ON
a.date=b.date AND a.user_id=b.user_id AND a.end_hour < b.start_hour WHERE
a.date='$date' AND a.user_id='$user_id' GROUP BY a.end_hour
HAVING a.end_hour < Min(b.start_hour);";
I need to create a range since 08:00 to 21:00 with the free blocks between the appointments like this:
free time
08:15:00 to 14:00:00
14:30:00 to 17:00:00
17:45:00 to 21:00:00
This will give you the end time blocks
This will give you the start time block
These queries will look for starts or ends that are uncrossed or butted up to other slots. so 10-11 11-12 will give 10 start, 12 end = free time is 08-10 and 12-21
So free time is 08:00 (unless its a start time) upto first 'start' time, then matching pairs of END->START upto last END->21:00 (unless 21:00 IS the last end)
N.B. You'll need to add the date in too.
Try this query
This will show these results
Also you must have the 21:00:00 in the table or you wont be able to get the last time difference. i entered 21:00:00 as start and end date in the table.
EDITED
This is modified query
And the result is
The points to learn from this query are
Perhaps this is over kill but this works on sql-server, not sure the effort to convert to mysql
Try this Query i hope its work for you.....