MYSQL UPDATE with IN and Subquery

前端 未结 5 1100
余生分开走
余生分开走 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条回答
  •  时光说笑
    2020-12-30 10:03

    Use:

    UPDATE entry 
       SET total_comments = (SELECT COUNT(*)
                               FROM COMMENTS c
                              WHERE c.eid = id
                           GROUP BY c.eid)
     WHERE id IN ( SELECT eid 
                     FROM comments 
                    WHERE id IN (1,2,3,4,5,6))
    

提交回复
热议问题