SQL语句//使用日期时间之间

问题描述 投票:0回答:1

我有以下查询:

select s.ScreenID, s.ScreenName, ps.ScheduleDate
from Screens s
left join PerformanceSchedules ps on ps.ScreenID = s.ScreenID
where s.TheatreID = 2 and ((ps.ScheduleDate not between convert(datetime, '2019-08-02 14:00:00.000') and convert(datetime, '2019-08-02 20:00:00.000')) or (ps.ScheduleDate is null))

查询的结果是:

ScreenID    ScreenName  ScheduleDate
4           Screen 1    2019-08-02 15:00:00.000
5           Screen 2    2019-08-02 15:00:00.000
6           Screen 3    NULL

通常它应该只返回屏幕3,而不是其他两个,因为两者的ScheduleDate是在时间跨度。

结果应该是:

6   Screen 3    NULL
sql sql-server ssms
1个回答
0
投票

我怀疑你真正想要的:

select s.ScreenID, s.ScreenName, ps.ScheduleDate
from Screens s left join
     PerformanceSchedules ps
     on ps.ScreenID = s.ScreenID and
        ps.ScheduleDate between convert(datetime, '2019-08-02 14:00:00.000', 121) and convert(datetime, '2019-08-02 20:00:00.000', 121)
where s.TheatreID = 2 and
      ps.ScreenID is null;

这将返回有那些时间之间没有计划日期的屏幕。

© www.soinside.com 2019 - 2024. All rights reserved.