日期:2014-05-18  浏览次数:20428 次

怎样定时执行一个表一个字段的统计啊?
例如,我想每天给user表统计今个星期的点击次数,点击次数在click表

select   @coun=count(*)   from   click   where   us_id=1   and   datediff(day,time,getdate())   <7
update   user   set   click=@coun   where   id=1

上次是把id=1的今个星期的点击次数更新了...
我怎样把整个user表的统计出来呢?

------解决方案--------------------
update a set
click= b.coun
from [user] a, (
select us_id, coun=count(*) from click where datediff(day,time,getdate()) <7
group by us_id
)b
where a.id=b.us_id
------解决方案--------------------
update user set click= ISNULL(A.icoun,0) from user left join
(
select useid, icoun=count(1) from click where datediff(day,time,getdate()) <7 group by userid
)A
on user.id=A.userid