比如:查询 24 小时之内上机的人员姓名列表。
表里有 BeginTime 和 EndTime ,
我是说,如果这哥们儿在 24 小时之前的上的机,现在还没下,我就无法得到他的姓名了。
这是最后一种情况了。我只是想把这个问题搞清楚。谢谢大家了。我写的 sql 是这样的:select Card.UserName as '24 小时之内上机的人员姓名列表' from Card
  join Record on (Card.ID = Record.CardID)
    where (Record.BeginTime between dateadd(hh,-24,getdate()) and getdate()) or
      (Record.EndTime between dateadd(hh,-24,getdate()) and getdate())请大家指教了,谢谢

解决方案 »

  1.   

    select Card.UserName as '24 小时之内上机的人员姓名列表' from Card
      join Record on (Card.ID = Record.CardID)
        where (Record.BeginTime between dateadd(hh,-24,getdate()) and getdate()) or
          (Record.EndTime between dateadd(hh,-24,getdate()) and getdate())
    or(Record.BeginTime <dateadd(hh,-24,getdate()) and Record.EndTime > getdate())
      

  2.   

    select Card.UserName as '24 小时之内上机的人员姓名列表' from Card
      join Record on (Card.ID = Record.CardID)
    and (dateadd(hh,-24,getdate()) between Record.BeginTime and Record.EndTime
    or getdate() between Record.BeginTime and Record.EndTime)
      

  3.   

    start1<start2 and end1 >end2
      

  4.   

    select Card.UserName as '24 小时之内上机的人员姓名列表' from Card
      join Record on (Card.ID = Record.CardID)
    and (dateadd(hh,-24,getdate()) between Record.BeginTime and Record.EndTime
    or getdate() between Record.BeginTime and Record.EndTime)建议不要用dateadd 在相关实际字段上建索引就可以了  不然你查询很慢的 而且数据多的时候 就你这个查询就G 了那么多dateadd
      

  5.   

    我写出来了,大概是这样的:
    只要再加一个 Computer 表里的正在使用字段(OnUse),就可以了。
    谢谢大家了。代码如下:select Card.UserName as '24 小时之内上机的人员姓名列表' from Card 
      join Record on (Card.ID = Record.CardID) join Computer on (Record.ComputerID = Computer.ID) 
        where ((Record.BeginTime between dateadd(hh,-24,getdate()) and getdate()) or 
          (Record.EndTime between dateadd(hh,-24,getdate()) and getdate()))
            and (Computer.OnUse = 1)
      

  6.   

    我觉得好像是因为 getdate() 是一直在变化的,所以系统判断不出来。
    开始以为 2 楼的方法没问题,可一试,出不来结果。恕我冒犯,还是我的方法,能够得到结果,而且逻辑也没错。
    就是,多查了几遍。呵呵。谢谢大家了。祝大家身体健康,万事如意!