上面的忽略了时间,改一下: update 表 set 状态='N' from 表 a where 时间=getdate() and exists(select 1 from 表 where 单位=a.单位 and 用户=a.用户 and 状态='Y' and 时间<getdate())
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
Update 表 set 状态 = 'N' where datediff(day,日期,getdate()) = 0 and 状态 = 'Y' and Exists(Select 1 from 表 where 状态 = 'Y' and 日期<getdate())
update 表 set 状态 = Y where 单位 ='a' and 用户名 ='b' and 日期 =getdate() and exists(select 1 from 表 where 日期<getdate() and 单位 ='a' and 用户名='b')
update 表 set 状态 = 'N' from 表 a where a.日期 =getdate() and exists(select 1 from 表 where 日期<getdate() and 单位 =a.单位 and 用户名=a.用户名 and a.状态 ='Y')
我居然写错了字段名,重新改一下:方法1. update 表 set 状态='N' from 表 a where 日期=getdate() and exists(select 1 from 表 where 单位=a.单位 and 用户名=a.用户名 and 状态='Y' and 日期<getdate())
方法2:update 表 set 状态='N' from 表 a inner join (select 单位,用户名 from 表 where 日期<getdate() and 状态='Y') b on a.单位=b.单位 and a.用户名=b.用户名 where a.日期=getdate()
update 表 set 状态 = 'N' from 表 a where a.日期 =getdate() and exists(select 1 from 表 where 日期<getdate() and 单位 =a.单位 and 用户名=a.用户名 and a.状态 ='Y')
to txlicenhe(马可&不做技术高手):你的方法会不会把所有用户的状态都更新了啊?to yujohny(踏网无痕) 、zjcxc(邹建) 谢谢,二位高手的方法我得慢慢研究一下,到底哪个速度更快呢?
很遗憾,基本上没有一个是正确的,大部分都在第一个条件语句中丢掉了 “状态='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'”,运行结果才正确,谢谢各位,辛苦了。
update 表 set 状态='N'
from 表 a where 时间=getdate()
and exists(select 1 from 表 where 单位=a.单位 and 用户=a.用户 and 状态='Y' and 时间<getdate())
and Exists(Select 1 from 表 where 状态 = 'Y' and 日期<getdate())
Update 表 set 状态 = 'N' where datediff(day,日期,getdate()) = 0
and 状态 = 'Y'
and Exists(Select 1 from 表 where 状态 = 'Y' and 日期<getdate())
set 状态 = Y
where 单位 ='a' and 用户名 ='b' and 日期 =getdate()
and exists(select 1 from 表 where 日期<getdate() and 单位 ='a' and 用户名='b')
set 状态 = 'N'
from 表 a where a.日期 =getdate()
and exists(select 1 from 表 where 日期<getdate() and 单位 =a.单位
and 用户名=a.用户名 and a.状态 ='Y')
update 表 set 状态='N'
from 表 a where 日期=getdate()
and exists(select 1 from 表 where 单位=a.单位 and 用户名=a.用户名 and 状态='Y' and 日期<getdate())
from 表 a inner join
(select 单位,用户名 from 表 where 日期<getdate() and 状态='Y') b
on a.单位=b.单位 and a.用户名=b.用户名
where a.日期=getdate()
set 状态 = 'N'
from 表 a where a.日期 =getdate()
and exists(select 1 from 表 where 日期<getdate() and 单位 =a.单位
and 用户名=a.用户名 and a.状态 ='Y')
谢谢,二位高手的方法我得慢慢研究一下,到底哪个速度更快呢?
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'”,运行结果才正确,谢谢各位,辛苦了。