table1 id userid count state
1 15 30 0
table2 id userid count datetime
1 15 20 xxxxxx
要求是:
每两分钟执行一次操作,table1的userid=table2中的userid,更新table2中count,table2中的count+table1中的count,同时更新table1中state为0的要变更为1。
这个如何来实现啊
------解决方案--------------------
-- 更新table2中count,table2中的count+table1中的count
update t2
set t2.[count]=t2.[count]+t1.[count]
from table2 t2
inner join table1 t1 on t2.userid=t1.userid
-- 同时更新table1中state为0的要变更为1
update t1
set t1.[state]=1
from table1 t1
where t1.[state]=0