-------------------------------------------------------
channel_id | program_id | start_time | end_time |
-------------------------------------------------------
1 | 1 | 9:00 | 10:00 |
-------------------------------------------------------
1 | 2 | 10:02 | 11:00 |
-------------------------------------------------------
1 | 3 | 11:10 | 11:30 |
-------------------------------------------------------
1 | 4 | 11:35 | 12:00 |
-------------------------------------------------------
1 | 4 | 12:00 | 12:30 |
-------------------------------------------------------
return $this->hasMany(ChannelProgram::class)->where('start','<', $date)->where('end','>', $date);
select "channels".*,
(select "start" from "channel_program" where "channel_id" = "channels"."id" and "end" < '2019-07-02 06:10:21' order by "start" desc limit 1) as "start_time_show",
(select "start" from "channel_program" where "channel_id" = "channels"."id" and "start" <= '2019-07-02 06:10:21' and "end" > '2019-07-02 06:10:21' limit 1) as "start_time_current",
(select "start" from "channel_program" where "channel_id" = "channels"."id" and "start" > '2019-07-02 06:10:21' limit 1) as "start_time_notShow"
from "channels" where "channels"."deleted_at" is null
public function scopeScheduleShows($query)
{
$date = \Carbon\Carbon::now();
$query->addSubSelect('start_time_show', ChannelProgram::select('start')
->whereColumn('channel_id', 'channels.id')
->where('end','<', $date)
->latest('start')
)->with('show.program');
}
public function scopeScheduleCurrents($query)
{
$date = \Carbon\Carbon::now();
$query->addSubSelect('start_time_current', ChannelProgram::select('start')
->whereColumn('channel_id', 'channels.id')
->where('start','<=', $date)
->where('end','>', $date)
)->with('current.program');
}
public function scopeScheduleNotShows($query)
{
$date = \Carbon\Carbon::now();
$query->addSubSelect('start_time_notShow', ChannelProgram::select('start')
->whereColumn('channel_id', 'channels.id')
->where('start','>', $date)
)->with('notShow.program');
}
Channel::scheduleShows()->scheduleCurrents()->scheduleNotShows()->paginate(10);
timestamp '2019-06-27 11:15:00'
/*
with channel_program as (
select 1 as channel_id, 1 as program_id, timestamp '2019-06-27 09:00:00' as start_time, timestamp '2019-06-27 10:00:00' as end_time union all
select 1 as channel_id, 2 as program_id, timestamp '2019-06-27 10:02:00' as start_time, timestamp '2019-06-27 11:00:00' as end_time union all
select 1 as channel_id, 3 as program_id, timestamp '2019-06-27 11:10:00' as start_time, timestamp '2019-06-27 11:30:00' as end_time union all
select 1 as channel_id, 4 as program_id, timestamp '2019-06-27 11:35:00' as start_time, timestamp '2019-06-27 12:00:00' as end_time union all
select 1 as channel_id, 4 as program_id, timestamp '2019-06-27 12:00:00' as start_time, timestamp '2019-06-27 12:30:00' as end_time
)
*/
select t.program_id
from (
select
program_id
,lag(start_time) over (order by start_time) lag_start_time
,lead(end_time) over (order by start_time) lead_end_time
from channel_program
) t
where timestamp '2019-06-27 11:15:00' between coalesce(t.lag_start_time, timestamp '1991-01-01 00:00:00')
and coalesce(t.lead_end_time, timestamp '2999-01-01 00:00:00')