我正在进入两天,并尝试查找输入的日期是否在特定用户之间或重叠。我似乎得到了表中不存在的EmployeeID的错误结果。
我在表格中的数据
EmployeeID LeaveStart LeaveEnd
1 2015-04-14 00:00:00.000 2015-04-14 00:00:00.000
1 2015-05-01 00:00:00.000 2015-05-02 00:00:00.000
如果我运行此查询,我会得到预期的结果,因为范围之间的日期已经在EmployeeID 1的表中
SELECT EmployeeID,LeaveStart,LeaveEnd from EmpLeaves where (LeaveStart >= '2015-04-30' and LeaveStart<= '2015-05-04') or (LeaveEnd between '2015-04-30' and '2015-05-04')
and employeeid=1
1 2015-05-01 00:00:00.000 2015-05-02 00:00:00.000
如果我为EmployeeID = 2运行相同的查询(表中不存在,我得到相同的结果),似乎是条件是返回true并忽略其余的,所以我可以把任何东西作为EmployeeID和我仍然得到相同的结果。某处我错过了什么。
SELECT EmployeeID,LeaveStart,LeaveEnd from EmpLeaves where (LeaveStart >= '2015-04-30' and LeaveStart<= '2015-05-04') or (LeaveEnd between '2015-04-30' and '2015-05-04')
and employeeid=2
1 2015-05-01 00:00:00.000 2015-05-02 00:00:00.000
如果我删除了LeaveEnd中的部分,我得到了预期的结果,但是我还需要找出DateEnd字段之间是否也有日期
SELECT EmployeeID,LeaveStart,LeaveEnd from EmpLeaves where (LeaveStart >= '2015-04-30' and LeaveStart<= '2015-05-04')
and employeeid=2
答案 0 :(得分:2)
对于您的逻辑,您需要修复where
子句中的括号:
SELECT EmployeeID, LeaveStart, LeaveEnd
from EmpLeaves
where ((LeaveStart >= '2015-04-30' and LeaveStart<= '2015-05-04') or
(LeaveEnd between '2015-04-30' and '2015-05-04')
) and
employeeid = 1
答案 1 :(得分:0)
排序答案是你在日期周围缺少一个括号。但是,你的目标可以通过这个来实现。
SELECT EmployeeID
, LeaveStart
, LeaveEnd
FROM EmpLeaves
WHERE (LeaveStart <= '2015-05-04'
OR LeaveEnd >= '2015-04-30')
AND EmployeeID = 1