简体   繁体   English

检查两次是否在同一天重叠

[英]Check if two times overlap if they are on the same Day

I have the following table. 我有下表。

CREATE TABLE IF NOT EXISTS mydb.Hours (
  Date DATE NULL,
  Start VARCHAR(5) NULL,
  End VARCHAR(5) NULL,
  Courses_ID INT NOT NULL,
  Classroom_ID INT NOT NULL,
  PRIMARY KEY (Courses_ID, Classroom_ID),
  INDEX fk_Hours_Classroom1_idx (Classroom_ID ASC),
  CONSTRAINT fk_Hours_Courses1
    FOREIGN KEY (Courses_ID)
    REFERENCES mydb.Courses (ID)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
 CONSTRAINT fk_Hours_Classroom1
   FOREIGN KEY (Classroom_ID)
   REFERENCES mydb.Classroom (ID)
   ON DELETE NO ACTION
   ON UPDATE NO ACTION)
ENGINE = InnoDB

I would like to check if before inserting there is already a course assigned to a classroom on that day with times that may overlap with the new entry. 我想检查一下,在插入之前是否已经在当天分配了某个教室的课程,其时间可能与新条目重叠。

I am not sure how I would create a BEFORE INSERT trigger to get the times for a classroom on that date and check if the times overlap as they are just a String of length 5. 我不确定如何创建一个BEFORE INSERT触发器来获取该日期教室的时间,并检查时间是否重叠,因为它们只是长度为5的字符串。


CREATE DEFINER = CURRENT_USER TRIGGER `mydb`.`Hours_BEFORE_INSERT` BEFORE INSERT ON `Hours` FOR EACH ROW
BEGIN
    if exists(select 1
    from Hours h
    where h.Classroom_ID = new.Classroom_ID
      and h.Date = new.Date
      and h.Start <= new.End
      and h.End   >= new.Start) then
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Room is already booked at that time';
      end if;
END

You can check the existence of an overlap with: 您可以通过以下方式检查是否存在重叠:

select 1
from Hours h
where h.Classroom_ID = @Classroom_ID
  and h.Date = @Date
  and h.Start <= @End
  and h.End   >= @Start;

Or run a conditional insert: 或运行条件插入:

insert into Hours (Date, Start, End, Courses_ID, Classroom_ID)
    select @Date, @Start, @End, @Courses_ID, @Classroom_ID
    from dual
    where not exists (
        select *
        from Hours h
        where h.Classroom_ID = @Classroom_ID
          and h.Date = @Date
          and h.Start <= @End
          and h.End   >= @Start;
    );

Then check if any row has been inserted. 然后检查是否已插入任何行。

Note: Replace the @-variables with the values of the new record. 注意:用新记录的值替换@-变量。

You can also check for overlap in a trigger and raise an error: 您还可以检查触发器中是否存在重叠并引发错误:

BEGIN
    IF EXISTS (
            select *
            from Hours h
            where h.Classroom_ID = new.Classroom_ID
              and h.Date = new.Date
              and h.Start <= new.End
              and h.End   >= new.Start
        )
        THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'course time overlap'
    END IF;
END

Note that you might also need to write a similar UPDATE trigger. 请注意,您可能还需要编写类似的UPDATE触发器。

Also note that you might want to change <= and >= to < and > , if you want to allow the overlap in exactly one minute (like 11:30- 12:30 and 12:30 -13:30 ). 另外请注意,您可能要更改<=>=<> ,如果你想允许恰好一分钟的重叠(如11:30- 12:3012:30 -13:30)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM