Checking if new interval overlaps - MySQL (or PHP)

前端 未结 2 777
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 11:20

I\'ve been thinking on how I can simplify the problem presented here. Complex MySQL Query - Checking for overlapping DATE intervals

At it\'s heart, minus all the fa

2条回答
  •  悲&欢浪女
    2021-01-19 11:51

    You can phrase an insert as:

    insert into schedules(start, end)
        select s, e
        from (select $start s, $end as e) t
        where not exists (select 1
                          from schedules s2
                          where s.start <= t.end and s.end >= t.start
                         );
    

    This will only insert the value if it doesn't overlap with an existing row in the table.

提交回复
热议问题