读书人

求一条sql语句!该怎么处理

发布时间: 2012-01-11 22:28:46 作者: rapoo

求一条sql语句!!!
有两个表,一个是用户表user(userID,commentCount),一个是评论表comment(commentID,addUserID)。我想更新用户表的所有用户的commentCount字段,通过评论表计算出评论数,这条sql语句应该这么写?


[解决办法]
update u
set
commentCount=(select isnull(count(*),0) from comment where addUserID=u.userID)
from
[user] u

[解决办法]
update user set commentCount=(select count(*) from comment where addUserID=a.userID) from user a

读书人网 >SQL Server

热点推荐