update 表 set 状态='N'
from 表 a where exists(select 1 from 表 where 单位=a.单位 and 用户=a.用户 and 状态='Y')

解决方案 »

  1.   

    上面的忽略了时间,改一下:
    update 表 set 状态='N'
    from 表 a where 时间=getdate()
     and exists(select 1 from 表 where 单位=a.单位 and 用户=a.用户 and 状态='Y' and 时间<getdate())
      

  2.   

    if (Select 状态 from 表 where datediff(day,日期,getdate()) = 0) = 'Y' 
        and Exists(Select 1 from 表 where 状态 = 'Y' and 日期<getdate())
        
    Update 表 set 状态 = 'N' where datediff(day,日期,getdate()) = 0
      

  3.   

    Update 表 set 状态 = 'N' where datediff(day,日期,getdate()) = 0
    and 状态 = 'Y' 
    and Exists(Select 1 from 表 where 状态 = 'Y' and 日期<getdate())
      

  4.   

    update 表 
    set 状态 = Y
    where 单位 ='a' and 用户名 ='b' and 日期 =getdate()
        and exists(select 1 from 表 where 日期<getdate() and 单位 ='a' and 用户名='b')
      

  5.   

    update 表 
    set 状态 = 'N'
    from 表 a where a.日期 =getdate()
        and exists(select 1 from 表 where 日期<getdate() and 单位 =a.单位 
                   and 用户名=a.用户名 and a.状态 ='Y')
      

  6.   

    我居然写错了字段名,重新改一下:方法1.
    update 表 set 状态='N'
    from 表 a where 日期=getdate()
     and exists(select 1 from 表 where 单位=a.单位 and 用户名=a.用户名 and 状态='Y' and 日期<getdate())
      

  7.   

    方法2:update 表 set 状态='N'
    from 表 a inner join 
    (select 单位,用户名 from 表 where 日期<getdate() and 状态='Y') b
    on a.单位=b.单位 and a.用户名=b.用户名
    where a.日期=getdate()
      

  8.   

    update 表 
    set 状态 = 'N'
    from 表 a where a.日期 =getdate()
        and exists(select 1 from 表 where 日期<getdate() and 单位 =a.单位 
                   and 用户名=a.用户名 and a.状态 ='Y')
      

  9.   

    to txlicenhe(马可&不做技术高手):你的方法会不会把所有用户的状态都更新了啊?to yujohny(踏网无痕) 、zjcxc(邹建)
    谢谢,二位高手的方法我得慢慢研究一下,到底哪个速度更快呢?
      

  10.   

    也谢谢 wstorm(小糊涂神)。我试了用  zjcxc(邹建) 第三个帖子的做法,速度比我自己写的快了四五倍,可出问题了,明明只有一条符合条件的记录,结果显示受到影响的行数为18行,问题可能就在最后那个 日期<getdate()上,用 日期=getdate()好象正好,可还没分析清楚。各位都辛苦了,再次谢谢!
      

  11.   

    很遗憾,基本上没有一个是正确的,大部分都在第一个条件语句中丢掉了 “状态='Y'”,最后用了zjcxc(邹建)的这一个:
    update 表 set 状态='N'
    from 表 a inner join 
    (select 单位,用户名 from 表 where 日期<getdate() and 状态='Y') b
    on a.单位=b.单位 and a.用户名=b.用户名
    where a.日期=getdate()我在最后的 where a.日期=getdate() 之后再加上一个条件 “状态='Y'”,运行结果才正确,谢谢各位,辛苦了。