检查教练的可用性

时间:2016-05-22 14:50:36

标签: sql sql-server inner-join

教练表:

enter image description here

巡回表:

enter image description here

创建一个查询,显示给定日期的可用教练列表
我坚持这个查询,我不知道如何继续:

SELECT
    c.coachID,
    c.coachNo
FROM 
    Tour t 
    inner join Coach c 
        on t.coachID=c.coachID 
WHERE 
    '10/01/2016' BETWEEN startDate AND (DATEADD(DAY,tourDuration,startDate)) 

输出:

coachID|coachNo
1      |WKS5623

1 个答案:

答案 0 :(得分:1)

我们可以通过以下方式获得可用的教练

  1. 找出现在正在巡回演出的教练

    SELECT DISTINCT CoachID FROM Tour WHERE '10/01/2016' BETWEEN startDate AND (DATEADD(DAY, tourDuration, startDate))

  2. 让那些不在教练名单中的教练现在参加巡回赛

    SELECT CoachId, CoachNo FROM Coach WHERE CoachId NOT IN ( SELECT DISTINCT CoachID FROM Tour WHERE '10/01/2016' BETWEEN startDate AND (DATEADD(DAY, tourDuration, startDate)) )

  3. 希望它可以帮助你。