简体   繁体   English

sql获取未在特定日期和时间预订的所有可用人员

[英]sql get all available people that are not booked on a particular date and time

I'm struggling with an SQL query. 我正在努力进行SQL查询。 I am building a booking system for a ski resort and in my database I have instructors and sessions. 我正在建立一个滑雪胜地的预订系统,在我的数据库中,我有很多教练和课程。 A session can have an instructor, and it has a date and startTime and endTime. 一个会话可以有一个讲师,并且它有一个日期,开始时间和结束时间。 In order to add a session, I want to get all available instructors for a chosen time and date. 为了添加会话,我希望获得选定时间和日期的所有可用讲师。 In other words, all instructors who don't have a booking on that date and at that time. 换句话说,所有在那个日期和那个时间都没有预订的教练。 Table example: 表示例:

eg instructors: i1, i2, i3, i4, i5, i6, i7, i8 例如讲师:i1,i2,i3,i4,i5,i6,i7,i8

sessions: 会议:

Instructor | 讲师| date | 日期| start | 开始 end | 结束|

**i1**          **2017-05-03**        **14:30:00**      **15:30:00**
**i2**          **2017-05-03**        **14:30:00**      **15:30:00**
**i3**          **2017-10-03**        **10:30:00**      **11:30:00**
**i4**          **2017-05-03**        **10:30:00**      **11:30:00**
**i1**          **2017-11-03**        **14:30:00**      **15:30:00**

Then for input date='2017-05-03' and start='14:30' and end='15'30' i want to get 然后对于输入date ='2017-05-03'和start = '14:30'和end ='15'30'我想要得到

i3,i4,i5,i6,i7,i8 I3,I4,I5,I6,I7,I8

Figured out that I need to left join session to instructors, group by instructor id and then eliminate those ids that have a field in the group with the selected inputs. 弄清楚了我需要离开与讲师的加入会话,按讲师ID分组,然后消除那些在组中具有选定输入的字段的ID。 However, for the GROUP BY clause, i have to use an aggregate function and i don't know which one could apply here. 但是,对于GROUP BY子句,我必须使用聚合函数,并且我不知道哪个函数可以在此处应用。

using not exists() 使用not exists()

select *
from instructors i
where not exists (
  select 1
  from sessions s
  where s.instructor = i.id
    and s.date  = '2017-05-03'
    and s.start = '14:30'
    and s.end   = '15:30'
    )

So I tried this query and apparently it works(at least for my test case) 所以我尝试了这个查询,显然它可以工作(至少对于我的测试用例而言)

Can anybody take a look and tell me if it looks correct? 有人可以看一下并告诉我它是否正确吗?

select * 
from instructor
where id in
(select id
from instructor 
group by id
having id not in 
(select distinct(instructorid) from Session
where date='2017-03-19' and starttime<='15:30:00' and endtime>='14:30:00') )

SirWinning's self-answer looks like it should work, but my version below removes some parts which weren't required. SirWinning的自我回答似乎应该可以使用,但是下面的我的版本删除了一些不需要的部分。

select * 
from instructor
where id not in 
  (select instructorid
   from Session
   where date='2017-03-19' and starttime<='15:30:00' and endtime>='14:30:00')

This code will find any instructors who aren't booked for a session which overlaps the 14:30-15:30 window on the relevant date. 该代码将查找在相关日期的14:30-15:30窗口重叠的所有未预定课程的讲师。

If that's what's wanted, then you're good to go. 如果那是想要的,那么您就很好了。 Of course it doesn't follow that the instructor is "really available". 当然,这并不能说明教师“确实有空”。 There could be other things which affect their availabilty (working hours, annual leave etc), so you'll need to ensure that there are things in place to handle such things. 可能还有其他因素会影响他们的工作能力(工作时间,年假等),因此您需要确保有适当的地方来处理这些事情。

Note also, that this code will prevent an instructor appearing available for "back to back" bookings. 还要注意,该代码将阻止讲师出现在“背对背”预订中。 If you want to allow a booking to start at 14:30 when another one ends at that time, you'll need to change the <= and >= to < and > . 如果您要允许另一时间结束的14:30开始预订,则需要将<=>=更改为<>

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

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