问题
This is my query.
("Select Count(*) From table1 where [t1] <= @TimeIn And [t2] >= @TimeOut;", cn)
My problem here is I can't compare other times. For example, if I input 7:00-9:00
then it will be saved as a new time and if I then input 8:00-10:00
it also save in my database how can I solve this I want to prevent the second input time from conflicting with the first?
回答1:
There are three combinations of checks you need to perform when looking for conflicting Time Windows.
- Is the TimeIn within an existing entry (Time Window)
- Is the TimeOut within an existing entry
- Does the combination of TimeIn and TimeOut wholly cover an existing entry
This Sql should cover those options
select
count(*) from table1
where
(
@timeIn > [T1] AND @timeIn < [T2]
) -- TimeIn in existing window
OR
(
@timeOut > [T1] AND @timeOut < [T2]
) -- TimeOut in existing window
OR
(
@timeIn < [T1] AND @timeOut > [T2]
) -- TimeIn and TimeOut wholly covers existing window
If you get a 0 returned, there are no conflicts
来源:https://stackoverflow.com/questions/56644537/detect-conflict-time-using-query-in-vb-net