Mysql:检查范围时间冲突

时间:2012-04-24 12:02:13

标签: php mysql codeigniter

我有下表,其中存储了教师课程例程

表名:batchroutine_regular

batchid class_day   subject    teacherid class_start   class_end
   1     Sunday     English-1    1001     02:00 PM     03:30 PM
   1     Sunday     Geography    1002     04:00 PM     05:30 PM
   2     Monday     Math         1001     10:00 AM     12:30 PM
   2     Tuesday    Geography    1001     01:00 PM     03:30 PM

现在假设您是teacherid1001的教师。我们要求您在星期一上课,该课程将从11:00 AM开始,到1:00 PM结束。现在我的问题是MYSQL / PHP查询是什么时候从表batchroutine_regular中找出你是否可用?

提前致谢:)

1 个答案:

答案 0 :(得分:4)

SELECT (COUNT(*) = 0) AS Available
FROM batchroutine_regular
WHERE
      teacherid = 1001
  AND class_day = 'Sunday'
  AND (
       (class_start <= MAKETIME(11,00,00) AND class_end >= MAKETIME(11,00,00))
    OR (class_start <= MAKETIME(13,00,00) AND class_end >= MAKETIME(13,00,00))
  )