SQL查询操作顺序

本文关键字:顺序 操作 查询 SQL | 更新日期: 2023-09-27 18:16:13

星期五。我的脑子坏了。这很简单,我很不好意思问这个问题:

我只想查询我的事件表(它具有非空的开始时间和可空的结束时间)。然而,对于我的单元测试,我一直得到2条记录(2:00和4:00的记录,而不仅仅是我所期望的2:00)

SELECT 
    EventId, 
    TaskId, 
    MachineId, 
    LoginId, 
    EventStartTimeUtc, 
    EventEndTimeUtc, 
    OpCode, 
    UnitId, 
    PositionId, 
    WebId, 
    Comment, 
    MakereadyCount, 
    GrossCount, 
    NetCount, 
    PerpetualGross, 
    PerpetualNet, 
    PerpetualMakeready, 
    TaskState, 
    EventTypeId, 
    IsAutoEvent, 
    IsTransferred, 
    LastUpdatedTimeUtc
FROM Event
WHERE MachineId = #MachineId# 
    AND EventStartTimeUtc >= #StartTimeUtc# 
    AND (EventEndTimeUtc IS NULL 
        OR ((EventEndTimeUtc IS NOT NULL) AND EventEndTimeUtc <![CDATA[<=]]> #EndTimeUtc#))
[Test]
public void ShouldSelectEventsInRange()
{
    //Arrange
    TaskDto testTask = _testRepository.CreateTask(new TaskDto { TaskId = 1234567 }, true);
    var machineId = ((ArtemisRepository)_testRepository).CreateMachine(123, "MR40SIM", "0V7", 200, 100, 555555); //Requires a 555555 down-task to exist in database
    EventRecordDto result = _testRepository.CreateEvent(new EventRecordDto {TaskId = testTask.TaskId, MachineId = machineId, EventStartTimeUtc = new DateTime(2014, 4, 15, 1, 50, 0), OpCode = "100", MakereadyCount = 1752, GrossCount = 5660, NetCount = 2512, Comment = "Test Event", IsAutoEvent = false, IsTransferred = false});
    EventRecordDto result2 = _testRepository.CreateEvent(new EventRecordDto {TaskId = testTask.TaskId, MachineId = machineId, EventStartTimeUtc = new DateTime(2014, 4, 15, 2, 0, 0), OpCode = "100", MakereadyCount = 1752, GrossCount = 5660, NetCount = 2512, Comment = "Test Event", IsAutoEvent = false, IsTransferred = false});
    EventRecordDto result3 = _testRepository.CreateEvent(new EventRecordDto {TaskId = testTask.TaskId, MachineId = machineId, EventStartTimeUtc = new DateTime(2014, 4, 15, 4, 0, 0), OpCode = "100", MakereadyCount = 1752, GrossCount = 5660, NetCount = 2512, Comment = "Test Event", IsAutoEvent = false, IsTransferred = false});
    //Act
    var results = _testRepository.SelectEventsInRange(machineId, new DateTime(2014, 4, 15, 2, 0, 0), new DateTime(2014, 4, 15, 3, 59, 59));
    //Assert
    Assert.IsTrue(results.Count == 1, "{0} records came, instead of the 1 record expeted!", new object[] { results.Count });
    Assert.IsTrue(results.Any(r => r.EventId == result2.EventId), "Expected Event (Id: {0}), Actual Event (ID: {1})", new object[] { result2.EventId, results[0].EventId});
}

SQL查询操作顺序

查询EventStartTimeUtc大于或等于2:00的记录,这两个记录都是,并且EventEndTimeUtc为空,这两个记录都是。

你的意思是EventStartTimeUtc,你有EventEndTimeUtc在where子句?