检查教授和房间的时间表是否有冲突

时间:2017-01-24 16:01:49

标签: mysql sql

我想检查教授是否有时间范围的预定主题,或者是否在该时间范围内有房间。这是我的架构:

| subjectcode | prof_id  | start               | end                 | room |
+---------------------------------------------------------------------------+
| FREE ELEC 4 | 16-00002 | 2016-12-12T07:30:00 | 2016-12-12T10:30:00 | 1104 |
| TECHNO      | 16-00001 | 2016-12-12T11:00:00 | 2016-12-12T14:00:00 | 1103 |
| FREE ELEC 4 | 16-00002 | 2016-12-13T08:00:00 | 2016-12-13T10:00:00 | 1104 |
| TECHNO      | 16-00001 | 2016-12-13T10:30:00 | 2016-12-13T13:30:00 | 1103 |
| IT ELEC 5   | 16-00002 | 2016-12-13T15:30:00 | 2016-12-13T18:30:00 | 1106 |
| MMS         | 16-00003 | 2016-12-15T08:00:00 | 2016-12-15T10:00:00 | 1107 |
+---------------------------------------------------------------------------+

我在下面有这个查询:

SELECT batchid, subjectcode, prof_id, DATE_FORMAT(start, '%Y-%m-%d %h:%i %p') AS start, DATE_FORMAT(end, '%Y-%m-%d %h:%i %p') AS end, room FROM schedules WHERE prof_id = "16-00002" OR room = "1103" OR start BETWEEN "2016-12-13T08:00:00" AND "2016-12-13T09:00:00" OR end BETWEEN "2016-12-13T08:00:00" AND "2016-12-13T09:00:00" OR "2016-12-13T08:00:00" BETWEEN start AND end

我会得到这个结果:

| subjectcode | prof_id  | start               | end                 | room |
+---------------------------------------------------------------------------+
| FREE ELEC 4 | 16-00002 | 2016-12-12T07:30:00 | 2016-12-12T10:30:00 | 1104 |
| TECHNO      | 16-00001 | 2016-12-12T11:00:00 | 2016-12-12T14:00:00 | 1103 |
| FREE ELEC 4 | 16-00002 | 2016-12-13T08:00:00 | 2016-12-13T10:00:00 | 1104 |
| TECHNO      | 16-00001 | 2016-12-13T10:30:00 | 2016-12-13T13:30:00 | 1103 |
| IT ELEC 5   | 16-00002 | 2016-12-13T15:30:00 | 2016-12-13T18:30:00 | 1106 |
+---------------------------------------------------------------------------+

我希望获得的结果与2016-12-13T08:00:002016-12-13T09:00:00的日期/时间范围相匹配,其中prof_id为16-00002 OR 房间为1104:< / p>

| subjectcode | prof_id  | start               | end                 | room |
+---------------------------------------------------------------------------+
| FREE ELEC 4 | 16-00002 | 2016-12-13T08:00:00 | 2016-12-13T10:00:00 | 1104 |
+---------------------------------------------------------------------------+

2 个答案:

答案 0 :(得分:2)

我真的不完全清楚您是如何过滤BETWEEN条件的,但主要问题是您在每个条件下都使用OR 。如果满足任何一个条件,则返回记录。如果您希望多个条件对于要返回的记录为真,则您需要使用某些分组条件并AND。看下面的内容......

SELECT 
  ...
FROM schedules 
WHERE 
  (prof_id = "16-00002" OR room = "1104") 
  AND 
  (start BETWEEN "2016-12-13T08:00:00" and "2016-12-13T09:00:00" 
  OR end BETWEEN "2016-12-13T08:00:00" and "2016-12-13T09:00:00" 
  OR "2016-12-13T08:00:00" BETWEEN start and end)

上述查询将返回A和B评估为true的所有结果:

 A) Professor is 16-00002, OR the room is 1104
 B) "start" is between 2016-12-13T08:00:00 and 2016-12-13T09:00:00,
    OR "end" is between 2016-12-13T08:00:00 and 2016-12-13T09:00:00,
    OR 2016-12-13T08:00:00 is between "start" and "end"

答案 1 :(得分:1)

我对mysql语法不是很精通,但一般查询会是这样的:

SELECT 
   batchid, subjectcode, prof_id, 
   DATE_FORMAT(start, '%Y-%m-%d %h:%i %p') AS start, 
   DATE_FORMAT(end, '%Y-%m-%d %h:%i %p') AS end, 
   room 
FROM 
   schedules 
WHERE 
   (prof_id = "16-00002" OR room = "1104")
   and (start <= "2016-12-13T08:00:00" AND "2016-12-13T09:00:00"<=end)