views:

103

answers:

5

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.

+1  A: 

That's exactly what I'd expect. The id is IN the set you give it, so total_comments = total_comments + 1.

It's not going to add one for each instance of the same value: that's not how IN works. IN will return a simple boolean yes/no.

Satanicpuppy
i know... but i want some method so that i can count number of entry.id in subqery. I can do it by running subquery sperate and then running update query using for loop for all those. But i wanna do it in one query thats my porblem :(
Johal
+1  A: 

Try:

UPDATE entry
  SET total_comments = (SELECT COUNT(*) 
                        FROM comments
                        WHERE entry.id = comments.eid
                        GROUP BY id)
VeeArr
this will run for all records in entry table but i want to run for only specified records thats why i used IN clause...
Johal
+2  A: 

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.

mluebke
+1: I completely agree, this is a better design choice than the current table structure.
OMG Ponies
+2  A: 

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))
OMG Ponies
yeah i am looking for something like that... my motive is to run update query based on subquery.... its to update "total_comments" in "entry" table with acutal number of comments, for id in "entry"if i run query that i have specified it only adds 1 for all number of comments found in subqery . Here what sql query turns into after subquery runs:UPDATE entry SET total_comments = total_comments + 1 WHERE id IN (1,1,1,2,2,1)so update runs only once for 1 and 2 id's in entry table. Coz its how IN works. But i want some how to run it 4 times for 1 and 2 times for 2.
Johal
The only way i think is to to run subquery something like that what i dont know how to do:UPDATE entry SET total_comments = total_comments + @count WHERE id IN ( SELECT eid, count(*) as @count FROM comments WHERE id IN (1,2,3,4,5,6))but then In doesnt work coz of multiple rows and also i dont know how to refer @count in set condition :(
Johal
A: 
UPDATE entry e 
    SET total_comments = ( SELECT COUNT(*) FROM comments WHERE eid = e.id)  
    WHERE 
    e.id in (SELECT eid FROM comments WHERE id IN (1,2,3,4,5,6))
Johal