在 Mysql 的 table 中查看不同时间的 date_column

Viewing the date_column with different hours in a table in Mysql

我有一个名为 shift 的 table,我想在其中查看 sdate 列,日期后跟班次的 start_time。本质上,我试图通过添加班次的 start_time 将经理 LB1 和 AE1 的这个日期分成 6 条记录。例如。 2017-08-12 08:00, 2017-08-12 09:00, 2017-08-12 10:00 经理 LB1。经理 AE1 也必须这样做 有没有一种方法可以这样查看 table 而无需更改它?

您可以使用 recursive common table expression,自 10.2.2 版起在 Maria DB 中可用:

with recursive cte as (
    select manager, sdate, start_time, end_time 
    from mytable
    union all
    select managear, sdate, start_time + interval 1 hour, end_time 
    from cte 
    where start_time < end_time - interval 1 hour
)
select manager, sdate, start_time, start_time + interval 1 hour end_time from cte

使用递归 cte returns 从 00:0023:00 的所有时间并加入 table:

with recursive cte as (
  select '00:00' time
  union all
  select concat(lpad(time + 1, 2, '0'), ':00') from cte
  where time + 0 < 23
)
select t.*, c.time 
from tablename t inner join cte c
on c.time >= t.start_time and c.time < t.end_time

参见demo
结果:

> Manager | sdate      | start_time | end_time | difference | time 
> :------ | :--------- | :--------- | :------- | ---------: | :----
> LB1     | 2017-08-12 | 08:00      | 14:00    |          6 | 08:00
> LB1     | 2017-08-12 | 08:00      | 14:00    |          6 | 09:00
> LB1     | 2017-08-12 | 08:00      | 14:00    |          6 | 10:00
> LB1     | 2017-08-12 | 08:00      | 14:00    |          6 | 11:00
> LB1     | 2017-08-12 | 08:00      | 14:00    |          6 | 12:00
> LB1     | 2017-08-12 | 08:00      | 14:00    |          6 | 13:00
> AE1     | 2017-08-12 | 14:00      | 20:00    |          6 | 14:00
> AE1     | 2017-08-12 | 14:00      | 20:00    |          6 | 15:00
> AE1     | 2017-08-12 | 14:00      | 20:00    |          6 | 16:00
> AE1     | 2017-08-12 | 14:00      | 20:00    |          6 | 17:00
> AE1     | 2017-08-12 | 14:00      | 20:00    |          6 | 18:00
> AE1     | 2017-08-12 | 14:00      | 20:00    |          6 | 19:00