Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
–
–
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.
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.
–
–
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))
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.