MYSQL UPDATE with IN and Subquery

Posted by Johal on Stack Overflow See other posts from Stack Overflow or by Johal
Published on 2010-06-07T20:58:37Z Indexed on 2010/06/07 21:02 UTC
Read the original article Hit count: 144

Filed under:
|
|
|

Hi i have tables like this :

table entry :

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

table comments :

id | eid | comment
___________
1 | 1 | comment sdfd
2 | 1 | testing testing
3 | 1 | comment text
4 | 2 | dummy comment
5 | 2 | sample comment
6 | 1 | fg fgh dfh

Query i write :

UPDATE entry SET total_comments = total_comments + 1 WHERE id IN ( SELECT eid FROM comments WHERE id IN (1,2,3,4,5,6))

Results i get is :

table entry :

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

Expected results :

table entry :

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

Any help will be appreciated.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about update