1

我必须下表, wherestart_timeend_timeareTIME字段:

 ____________________________________
| id | day | start_time |  end_time  |
|____|_____|____________|____________|
|  1 | 1   |    8:00    |    12:00   |

我想获得 和 之间每 60 分钟间隔的开始时间start_timeend_time如:

 _______
| start |
|_______|
|  8:00 |
|  9:00 |
| 10:00 |
| 11:00 |

那可能吗?谢谢

4

1 回答 1

1

如果您有一个包含整数的表,这会更容易。如果该字段是日期时间,您可以这样做(最多 7 小时):

select date_add(t.start_time, interval n.n hour)
from t join
     (select 0 as n union all select 1 union all select 2 union all select 3 union all
      select 4 union all select 5 union all select 6 union all select 7
     ) n
     on date_add(t.start_time, interval n.n hour) <= t.end_time

使用time,您可以通过转换为秒并在那里进行算术来做到这一点:

select sec_to_time((floor(time_to_sec(t.start_time - 1)/3600)+1) + n.n*3600)
from t join
     (select 0 as n union all select 1 union all select 2 union all select 3 union all
      select 4 union all select 5 union all select 6 union all select 7
     ) n
     on (floor(time_to_sec(t.start_time - 1)/3600)+1) + n.n*3600 <= t.end_time
于 2013-06-19T13:47:36.387 回答