简体   繁体   English

SQL语句//与日期时间之间

[英]SQL Statement // between with datetime

I have following query: 我有以下查询:

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

The result of the query is: 查询的结果是:

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

Normally it should only return Screen 3 and not the other two, because the ScheduleDate of the two is in the time span. 通常,它应该只返回屏幕3,而不返回其他两个,因为两者的ScheduleDate在时间范围内。

The result should be: 结果应为:

6   Screen 3    NULL

I suspect you really want: 我怀疑你真的想要:

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;

This will return the screens that have no schedule date between those times. 这将返回在这些时间之间没有计划日期的屏幕。

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

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