Mysql: checking range time conflicts [closed]

2019-07-17 11:55发布

问题:

I have the following table where teachers class routine are stored

Table Name: 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

Now lets say you are the teacher whose teacherid is 1001. You are asked to take a class on Monday which will start at 11:00 AM and end at 1:00 PM. Now my question is what would be the MYSQL/PHP query to find out if you are available at that time, from the table batchroutine_regular?

Thanks in Advance :)

回答1:

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))
  )