代码之家  ›  专栏  ›  技术社区  ›  Atif

mysql从其他表更新数据

  •  1
  • Atif  · 技术社区  · 15 年前

    我有两张这样的桌子:

    content (content_id, content_type, user_id, time, comment_count)
    
    comments (comment_id, content_id, userid, comment, comment_time)
    

    我想做的是用comments表中的comments的总和即count(content_id)更新comments_count字段。

    我无法找出正确的语法

    2 回复  |  直到 8 年前
        1
  •  2
  •   unutbu    15 年前
    UPDATE content c1 SET comment_count=(
        SELECT COUNT(c2.content_id) FROM comments c2 
        WHERE c1.content_id = c2.content_id
    )
    
        2
  •  0
  •   Michael Shnitzer    15 年前

    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'