Bluehost.com Web Hosting $6.95

MySQL: Count comments (Distinct) on many to many pair userid/topicid

This is a discussion on MySQL: Count comments (Distinct) on many to many pair userid/topicid within the MySQL Database forums, part of the Database Forums category; I have a mySql table like this userid (int) topicid (int) text (varchar) It stores comments for a small forum. ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-21-2007
aNickname
 
Posts: n/a
Default MySQL: Count comments (Distinct) on many to many pair userid/topicid

I have a mySql table like this

userid (int)
topicid (int)
text (varchar)

It stores comments for a small forum.
Table entries may be like this, to give an idea

20 18 'hi'
20 18 'hello to you'
20 18 'how are you?'
20 18 'fine'
20 654 'i dont like it'
20 654 'i like it'
310 87 'i prefer green'
310 87 'i prefer red'
310 18 'i dont know it' <-- note this: different userif but the topic
id can be prsent for userid 20 too

So, it is basically the "tuple" userid topicid that identifies a
member space.

Now, my question is: regardless of which user (that is, I want to know
for all users) and regardless of specific topicid (that is, I want to
know for all topicids) how can I return: (userid, topicid, COUNT of
comments for that "tuple") WITHOUT having duplicates and being sure
the count of comments per each topic/user pair is correct?

I tried in several fashions with distinct, count, group by but i
always ended either with duplicates or with wrong counts.
Please note I don't need only the count but the 3 data: userid,
topicid count(how many comments for that couple, no duplicates)

Any idea? I'm not an expert and neither a beginner, but this quesry is
really giving troubles to me and I can't trust the fact in one
occasion seemed right: I need be sure it's right, I can't code by
chance :-)

thanks

Reply With Quote
  #2 (permalink)  
Old 03-22-2007
strawberry
 
Posts: n/a
Default Re: MySQL: Count comments (Distinct) on many to many pair userid/topicid

aNickname wrote:
> I have a mySql table like this
>
> userid (int)
> topicid (int)
> text (varchar)
>
> It stores comments for a small forum.
> Table entries may be like this, to give an idea
>
> 20 18 'hi'
> 20 18 'hello to you'
> 20 18 'how are you?'
> 20 18 'fine'
> 20 654 'i dont like it'
> 20 654 'i like it'
> 310 87 'i prefer green'
> 310 87 'i prefer red'
> 310 18 'i dont know it' <-- note this: different userif but the topic
> id can be prsent for userid 20 too
>
> So, it is basically the "tuple" userid topicid that identifies a
> member space.
>
> Now, my question is: regardless of which user (that is, I want to know
> for all users) and regardless of specific topicid (that is, I want to
> know for all topicids) how can I return: (userid, topicid, COUNT of
> comments for that "tuple") WITHOUT having duplicates and being sure
> the count of comments per each topic/user pair is correct?
>
> I tried in several fashions with distinct, count, group by but i
> always ended either with duplicates or with wrong counts.
> Please note I don't need only the count but the 3 data: userid,
> topicid count(how many comments for that couple, no duplicates)
>
> Any idea? I'm not an expert and neither a beginner, but this quesry is
> really giving troubles to me and I can't trust the fact in one
> occasion seemed right: I need be sure it's right, I can't code by
> chance :-)
>
> thanks


I don't quite understand. What result would you expect from the data
snippet provided?

Is this right?

20 18 4

20 654 2

310 87 2

310 18 1

Or some other result?

Reply With Quote
  #3 (permalink)  
Old 03-22-2007
aNickname
 
Posts: n/a
Default Re: MySQL: Count comments (Distinct) on many to many pair userid/topicid

I found it.
I share the solution in case someone else may benefit from it

GROUP BY userid, topicid

In that way, it extracts only the _combinations_ of user and topic
that are unique. Count is then applied on those combinations, without
duplicates. Having used GROUP BY thus far only on one column, I didn't
realize actually that grouping by more fields, it does not group
anymore by COLUMN but by ROW, which was what the case required.

Thank you for your help anyway!

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:26 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0