tags:

views:

44

answers:

3

There are two tables. One is users info "users", one is comments info "comments".

I need to create new field "comments" in users table, that contains number of comments of that user. Table "comments" has "user" field with user's id of that comment.

What is optimal way to count number of comments by every user so far?

With php you should write script that selects every user and than count number of his comments and then update "comments" field. It is not hard for me, but boring.

Is it possible to do it without php, only in MySQL?

+1  A: 

Why do you want to store it there anyway? Why not just show it combined query?

select users.name, count(comments.id) as comment_count
from users
join comments on users.id=comments.user_id
group by users.id

If you want to do it your way then include

update users set comment=comment+1 where id=$user_id

into the script where you store the comment.

And

update users set comment=comment-1 where id=$user_id

into the place where user can delete his comment. Otherwise your data might be out of sync when user adds new commnts and you haven't run the script yet.

Riho
Qiao
@Qiao no, it is not "just easy".
Col. Shrapnel
A: 
UPDATE TABLE users SET CommentCount = (SELECT COUNT(*) FROM comments WHERE AuthorUserId = users.id)
Andreas Bonini
Old good SO-way automated answer.
Col. Shrapnel
+1  A: 

Yes, it is possible. This is called table joining. You don't add another field to the users table, but to the resulting table.

SELECT users.*, count(comments.id) as num_comments 
FROM users,comments 
WHERE comments.cid=users.id 
GROUP BY users.id

Such a query is what relational databases were invented for. Do not revert it to the plain text file state. There is many reasons to make it that way.
http://en.wikipedia.org/wiki/Database_normalization <-- good text to read

Col. Shrapnel
This is not what he asked though =p There are serious performance advantages to using his approach (an additional field)
Andreas Bonini
but the data may be wrong unless you run it every time new comment is entered
Riho
@Andreas premature optimisation is the root of all evil. Ever heard that? Denormalisation must be done with heavy reasons, and "i-know-nothing" is not among these reasons.
Col. Shrapnel
@Riho: that's why they invented triggers.
Andreas Bonini
it is easy to make several 'not optimized' fields, than handle big fancy queries. It is much easy to see real data. It is "Rule of Representation: Fold knowledge into data, so program logic can be stupid and robust."
Qiao
@Qiao ahaha such a wrong conclusion from the right rule :) You make your logic way harder that way. But it's up to you. You are free to shoot yourself in the leg. Good lock :)
Col. Shrapnel
@Col. Shrapnel Ok, so everybody who make community software is wrong and you are right :) But of course it "all depends...". Besides, shooting in the leg is good.
Qiao
@Qiao don't talk of community software as you know none
Col. Shrapnel
@Col. Shrapnel may be i am wrong, but i will know it only after trying it. "community software", for example, my favorite forum MyBB has "postnum" in "users" table. SO has "CommentCount" in "posts" - http://meta.stackoverflow.com/questions/2677/anatomy-of-a-data-dump/2678#2678
Qiao
@Bonini - not everyone has access to triggers. Some webhostings allow you only to mess around with tables
Riho
@Qiao If you insist in doing it in your way then depending from the frequency you run this query the COUNT() approach is overkill or does not reflect the current data. Use the triggers like Bonini suggested.
Riho
@Qiao You do not know why they did it, what did it cost and if you have same case. Every case is different. And you going to do something not because you need it but just in case.
Col. Shrapnel
@Riho i will use `SET field = field+1` after that. I am not that stupid to run this query every time )
Qiao
@Col. Shrapnel, no I am not doing this 'just in case'. I am doing this because there are real reasons to thinks that this has advantages. If it has not, i will just change it. If you know other way to learn stuff without trying it, let me know :)
Qiao
@Qiao Of course. Book reading.
Col. Shrapnel
@Qiao not only `SET field = field+1` but also `SET field = field-1` in many places which you cannot predict at all - say, bulk deleting. and if there will be some ACL or just hidden or friends comments - you're in trouble. You don't understand atomic data paradigm which is cornestone of the databse you're using.
Col. Shrapnel