我在mysql中有一个这样的表:
+------------+------------+------------+------------+
| 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 |
+------------+------------+------------+------------+
我尝试提取我在这里找到的用户的时间范围和示例,但我无法在小时内完成这项工作
我试过查询:
$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);";
我需要创建一个从 08:00 到 21:00 的范围,其中约会之间的空闲块如下所示:
free time
08:15:00 to 14:00:00
14:30:00 to 17:00:00
17:45:00 to 21:00:00