MYSQL UPDATE with IN and Subquery

前端 未结 5 1131
余生分开走
余生分开走 2020-12-30 09:18

Hi i have tables like this :

table entry :

id | total_comments
_____________________
1 | 0
2 | 0
3 | 0
4 | 0

5条回答
  •  猫巷女王i
    2020-12-30 10:09

    If you really need total_comments in a separate table, I would make that a VIEW.

    CREATE VIEW entry AS 
      SELECT id, COUNT(comments) AS total_comment 
      FROM comments 
      GROUP BY id
    

    This way you avoid the maintenance task of updating the total_comments table altogether.

提交回复
热议问题