gpt4 book ai didi

mysql - 存储过程返回错误值?

转载 作者:可可西里 更新时间:2023-11-01 08:05:52 26 4
gpt4 key购买 nike

我有一个存储过程来检查在我的数据库中预订的假期,它不应该考虑状态为“已拒绝”或“已取消”的假期,但我的数据库中只有一个记录拒绝状态但此选择语句返回 1?

        SELECT COUNT(*) JobRoleID
FROM Employees
RIGHT JOIN Holidays
ON Employees.ID = Holidays.EmployeeID
WHERE Holidays.Status <> 'Declined' AND Holidays.Status <> 'Cancelled'
AND (Holidays.Startdate <= '2014/04/28' AND Holidays.Enddate >= '2014/04/30')
OR (Holidays.Startdate >= '2014/04/28' AND Holidays.Enddate <= '2014/04/30')
OR (Holidays.Startdate <= '2014/04/30' AND Holidays.Enddate >= '2014/04/30')
OR (Holidays.Startdate <= '2014/04/28' AND Holidays.Enddate >= '2014/04/28')
OR (Holidays.StartDate = '2014/04/28' AND Holidays.EndDate = '2014/04/30')

最佳答案

这里需要考虑运算符的优先级。

Not -->  AND --> OR 

Not 优先于 AND ,AND 优先于 OR。对于这些嵌套的 AND 和 OR,最好将它们放在括号中。

WHERE       Holidays.[Status] <> 'Declined' AND Holidays.[Status] <> 'Cancelled'
AND
( (Holidays.Startdate <= '2014/04/28' AND Holidays.Enddate >= '2014/04/30')
OR (Holidays.Startdate >= '2014/04/28' AND Holidays.Enddate <= '2014/04/30')
OR (Holidays.Startdate <= '2014/04/30' AND Holidays.Enddate >= '2014/04/30')
OR (Holidays.Startdate <= '2014/04/28' AND Holidays.Enddate >= '2014/04/28')
OR (Holidays.StartDate = '2014/04/28' AND Holidays.EndDate = '2014/04/30')
)

如果在 sql 中使用方括号 [] 时,也请避免使用 Sql server 关键字作为列名。

关于mysql - 存储过程返回错误值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22839415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com