开发者

MYSQL updating data from other table

开发者 https://www.devze.com 2022-12-28 01:16 出处:网络
I have two tables like of this structure: con开发者_开发百科tent (content_id, content_type, user_id, time, comment_count)

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

精彩评论

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