读书人

更新article表的CommentsCount字段(SQ

发布时间: 2013-03-16 11:51:46 作者: rapoo

【求助】更新article表的CommentsCount字段(SQL CE4)
如题,牵扯到两张表:articles表 和 comments表

【环境】
WebMatrix 2 + SQL CE 4

【需求】
不小心把articles中commentsCount字段所有记录置为0;

现在想根据comments表中的articleId字段统计count,来更新articles表的commentsCount字段。

【我的SQL语句】
UPDATE articles
SET articles.commentsCount=
(select count(*) FROM comments WHERE comments.articleId = articles.articleId)

【错误提示】
“分析查询时出错。 [ Token line number = 3,Token line offset = 2,Token in error = select ]”


求指点!!!!!


update SQL?CE4
[解决办法]
这个语法在05、08上都是对的~
换种方法呢:

UPDATE a SET a.commentsCount=isnull(b.commentsCount,0)
from articles a left join
(select articleId,(select count(*) FROM comments WHERE comments.articleId = articles.articleId) as commentsCount from articles )b on a.articleId=b.articleId

读书人网 >SQL Server

热点推荐