我有一个表,里面有姓名、状态(出、入)和刷卡时间等字段。现在我要查询当前不在房屋里的人,即
"select * from InOut where 状态='出' and 姓名='张三' and 刷卡时间<='" & now & "'" ,由于张三之前进进出出不知多少次,但这个时间张三已经返回房屋,所以这个查询只能查询出张三在这个时间前处于'出'状态的记录,因此这个SQL语句是错误的。
我想了想,因为状态是成对出现的,能不能统计这个状态是否是2的倍数来做查询条件。各位高手,帮帮我啊.

解决方案 »

  1.   


    select * from InOut where 状态='出' and 姓名='张三' and datadiff(mi,刷卡时间,getdate())>0
      

  2.   

    其实应该是这样写的 "select * from InOut where 状态='出' and 刷卡时间 <='" & now & "'" 
    由于张三此前进出过不知多少次,所以这个查询张三也位列其中,但此时张三已经回到房屋,因此当前不在房屋里的人不应该包含张三。
      

  3.   

     <='" & now & "'"  你这是什么东西?
      

  4.   

    不是SQL语句里语法的问题,now 是获取的当前系统时间
      

  5.   


    select 姓名 ,[状态]=case when count(状态)%2=0 then '出' else '进' end 
    from Tb 
    group by  姓名 
      

  6.   


    declare @a int ,@b datetime,@c nvarchar(500)
    select @a=count(*) from InOut where  姓名='张三'
    select @b=max(刷卡时间)from InOut where  姓名='张三'
     if @a%2=0
    set @c='此人已出去,出去时间为'+cast(datediff(mi,@b,getdate()) as nvarchar)+',时间过长,正在某处偷懒~~'
    print @c
    else
    set @c='此人已进去,离刷卡进入的时间为'+cast(datediff(mi,@b,getdate()) as nvarchar)+',时间较短,可能刚偷懒回来~~'
    print @c
    楼主思路对的。。判断他一天内刷了几次卡。。偶数表示他出去了。基数表示在屋里。。没必要收他出去的记入,知道了记入,也不知道他在不在屋里。。我这详解。。
      

  7.   

    declare @a int ,@b datetime,@c nvarchar(500)
    select @a=count(*) from InOut where  姓名='张三'
    select @b=max(刷卡时间)from InOut where  姓名='张三'
     if @a%2=0
    set @c='此人已出去,出去时间为'+cast(datediff(mi,@b,getdate()) as nvarchar)+'分钟,时间过长,正在某处偷懒~~'
    print @c
    else
    set @c='此人已进去,离刷卡进入的时间为'+cast(datediff(mi,@b,getdate()) as nvarchar)+'分钟,时间较短,可能刚偷懒回来~~'
    print @c少了分钟两字。。哈哈。。
      

  8.   

    提供个思路吧,需要查询 的用户最后一次状态是out就是不在了。