我在SQL-Server2005和Classic ASP中遇到了一个令人困惑的小问题。我在数据库中有以下表
+-----------+----------+----------+--------------+-------------------------+-------------------------+--------------+
| ProgramID | SystemID | ClientID | ProgramName | ProgramStart | ProgramEnd | ProgramHours |
+-----------+----------+----------+--------------+-------------------------+-------------------------+--------------+
| 22 | 18 | 4 | After Gym | 1900-01-01 09:00:00.000 | 1900-01-01 11:00:00.000 | 2hrs 0mins |
| 23 | 18 | 4 | Free Weights | 1900-01-01 12:00:00.000 | 1900-01-01 14:00:00.000 | 2hrs 0mins |
+-----------+----------+----------+--------------+-------------------------+-------------------------+--------------+这基本上显示了在特定时间段内分配给系统和客户端的程序。我想要做的是防止用户进入与已经使用的时间重叠的第三个程序(例如上午10点到11点)。创建页面具有小时、分钟和开始时间的上午/下午的下拉菜单,以及结束时间的3个以上的下拉菜单。我想要做的是触发一个ajax脚本,当下拉菜单选择发生变化时,该脚本会查找重叠的条目。如果找到一个,它会触发一个js警告。
如何检测SQL中的这种重叠?
任何想法都会受到极大的欢迎。
非常感谢,保罗
发布于 2010-08-31 21:01:48
SELECT * FROM appointments WHERE
(time_from <= $from and time_to >= $to) or // determine if the new appointment is fully in an exitent
(time_from <= $from and $from < time_to and time_to <= $to) or // determine if the new appointment starts in an existing and ends after
(time_from >= $from and time_to >= $to and $to > time_from) or // determine if the new appointment starts before an existing and ends in
(time_from > $from and time_to < $to) // determine if the new appointment starts before and ends after如果选择了更多任何记录,则会出现重叠
编辑:我更正了这句话。它现在应该可以工作了。
发布于 2010-08-31 20:59:01
您可以执行select操作,查看输入的日期是否在任何范围内:
SELECT COUNT(1) as tCount FROM TABLE WHERE programStart <= formDate AND programEnd <= formDate如果它以>=1的形式返回tCount,则存在重叠。
此外,你的程序长度不应该真的存储在数据库中,因为它在技术上是重复的数据,每次你改变程序时间都需要更新,我建议在需要的时候提取它的值(例如使用SQL dateDiff() )。
发布于 2010-09-02 00:26:00
我最终用下面的SQL语句解决了这个问题:
SELECT COUNT(*) AS Program_Count FROM Programs
WHERE ('1900-01-01 10:00:01.000' BETWEEN ProgramStart AND ProgramEnd)
OR ('1900-01-01 10:59:59.000' BETWEEN ProgramStart AND ProgramEnd)
OR (ProgramStart BETWEEN '1900-01-01 10:00:01.000' AND '1900-01-01 10:59:59.000')
OR (ProgramEnd BETWEEN '1900-01-01 10:00:01.000' AND '1900-01-01 10:59:59.000')
AND SystemID = 18;"感谢您的所有建议,当结合和研究时,给我带来了这个解决方案。
https://stackoverflow.com/questions/3609222
复制相似问题