0

我有以下 SQL 语句来返回特定模块的行存在日期冲突的行:

SELECT e1.[id], e1.[start], e1.[end], e2.[id] AS Expr1, e2.[start] AS Expr2, e2.[end] AS Expr3
FROM ClassTimes AS e1 
INNER JOIN ClassTimes AS e2 ON e1.[id] < e2.[id]
WHERE (e1.[start] BETWEEN e2.[start] AND e2.[end])
AND (e1.[module] = @module) 
AND (e2.[module] = @module) 
OR  (e1.[module] = @module) 
AND (e2.[module] = @module) 
AND (e1.[end] BETWEEN e2.[start] AND e2.[end])

对于某些冲突,这很有效,例如我的数据库中将返回的以下行:

id| start            | end              | module                 
----------------------------------------------------
1 | 30/02/2016 10:00 | 30/02/2016 11:00 | 1
2 | 30/02/2016 10:30 | 30/02/2016 11:30 | 1

但是我遇到的问题是,当我有一行的起始值是另一行的等效结束值时。我在下面显示了 30/02/2016 13:00 的值。

id| start            | end              | module                 
----------------------------------------------------
3 | 30/02/2016 12:00 | 30/02/2016 13:00 | 1
4 | 30/02/2016 13:00 | 30/02/2016 14:00 | 1

如何修改我的 sql 语句以消除返回这些端点的行?

4

3 回答 3

0

如果我理解它,您希望同时获得 2 个模块的所有行。

SELECT e1.[id], e1.[start], e1.[end], e2.[id] AS Expr1, e2.[start] AS Expr2, e2.[end] AS Expr3
FROM ClassTimes AS e1 
INNER JOIN ClassTimes AS e2 ON e1.[module] = e2.[module] AND e1.[id] < e2.[id]
WHERE e1.[module] = @module AND ((e1.[start] BETWEEN e2.[start] AND e2.[end])
    OR  (e1.[end] BETWEEN e2.[start] AND e2.[end]))
于 2016-03-31T15:02:35.403 回答
0

我相信这现在对我有用:

WHERE (e1.[module] = @module) 
AND (e2.[module] = @module) 
AND (e1.[end] > e2.[start]) 
AND (e1.[end] <= e2.[end]) 
OR  (e1.[module] = @module) 
AND (e2.[module] = @module) 
AND (e1.[start] >= e2.[start]) 
AND (e1.[start] < e2.[end])
于 2016-03-31T14:29:04.953 回答
0

看看这个。介于范围内的值包括在内。

改用“<”和“>”,这将解决您的问题

于 2016-03-31T13:41:15.720 回答