开发者

MYSQL updating data from other table

I have two tables like of this structure:

con开发者_开发百科tent (content_id, content_type, user_id, time, comment_count)

comments (comment_id, content_id, userid, comment, comment_time)

What I wold like to do is update the comments_count field with sum of comments i.e COUNT(content_id) from the comments table.

I am not able to figure out the right syntax


UPDATE content c1 SET comment_count=(
    SELECT COUNT(c2.content_id) FROM comments c2 
    WHERE c1.content_id = c2.content_id
)


Cross Table Update with MySQLCross Table Update with MySQL

UPDATE product p, productPrice pp
SET pp.price = pp.price * 0.8
WHERE p.productId = pp.productId
AND p.dateCreated < '2004-01-01'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜