在sql server上有where列的where子句

时间:2011-10-12 15:06:09

标签: asp.net sql time

在项目中,有一项功能可让您在白天预订地点,他们必须输入当天,开始时间和最后时间,从上午7:00到凌晨12:00(午夜)或凌晨1:00例如,如果有人输入date = 21 / oct / 2011开始时间=晚上8:00和结束时间12:00 am(他将该地点从晚上8点预订到凌晨12:00),webform将发送到商店程序20:00和00:00查看表格,看看是否可用,如果有人已经在同一天预订到午夜它是这样的商店 startime = 23:00 endtime = 00:00

所以当我检查新客户时,它必须返回已经在时间范围内的预订, 我的查询效率不高但是它从7:00到23:00工作,当从webfrom进入上午12点(sql上的00:00)时它失败,因为starttime es大于结束时间

这是我的查询

select COUNT(*)
    from table1
    where id_place=@id_place
    and date=@date  
       and (
           (@start_time=res_start_time and @end_time=res_end_time)
        or (@start_time > res_start_time and @start_time < res_end_time)
        or (@end_time > res_start_time and res_end_time < res_end_time)
        or (res_start_time > @start_time and res_start_time < @end_time)
        or (res_end_time > @start_time and res_end_time < @end_time)
        or (res_start_time < @start_time and res_end_time >@end_time)
          )     


    -- @start_time  = start time of the reservations (from webform)
    -- @end_time    = end time of the reservations (from webform)

    -- res_start_time= represents the start time column
    -- res_ebd_time= represents the end time column

我需要两个方面的帮助,当我必须检查上午12点或凌晨1点这些已经在表格中的时间(如问题开头的示例)并检查我时,我是如何解决问题的查询因为认为必须有更好的解决方案来实现这种功能

1 个答案:

答案 0 :(得分:1)

你显然在表格的某个地方也有约会,或者你无法预订资源。这导致了几种可能性。

  1. 查询时间和日期(可能会变得复杂,但您可以将其作为重用的udf)
  2. 使用DateTime而不是日期和时间列
  3. 如果在SQL Server中,考虑创建一个CLR函数来处理这个问题,因为CLR(.NET代码)将更有效地确定“在时间范围内”
  4. 我相信还有其他可能性。这里的关键点是听起来你的算法失败很大程度上是由于事件蔓延到另一天。如果这是正确的,那么把这一天带入等式是你最好的选择。这最终可能是一个相当复杂的SQL语句,数据类型的更改(日期时间而不是日期和时间)或创建.NET函数(CLR)以帮助更有效地确定“在范围内”。