tags:

views:

30

answers:

2

Sorry for the crappy title - didn't know how else to say it.

I have a commenting database where anyone can report a comment for abuse (anyone as in not just logged in members). This obviously means a person could report a comment multiple times. Where I need help is building a query that shows the number of times a comment has been reported but by unique email address AND ip address.

I'm not sure if that is clear or not -- if [email protected] of IP address 1.2.3.4 reports a message more than once, it will only count as one report. If there is an additional record of [email protected] with an IP of 4.5.6.7 reporting the same comment, it would count as two reports since the IPs are different.

The fields, all in one table:

  • id
  • comment_id
  • note
  • email
  • ip_address

I hope that all makes sense and any help would be GREATLY appreciated!

+2  A: 
SELECT  email, ip_address, COUNT(DISTINCT note)
FROM    log
GROUP BY
        email, ip_address

Update:

As per your comment, I believe you want this:

SELECT  comment_id, COUNT(DISTINCT email, ip_address)
FROM    log
GROUP BY
        comment_id
Quassnoi
What does the (DISTINCT note) do versus Daniel's (*)?
86Stang
If `joe` submits three reports: "spam", "spam", "ham"; "spam" will be counted only once.
Quassnoi
No, it will be counted twice.... (spam + ham)
Wrikken
@Wrikken: read my comment again, please.
Quassnoi
You updated query worked like a champ. Thanks so much!I love S.O.!!
86Stang
Pff, reading is an art, you are indeed correct, my apologies, seems to stem from the weird thing by ending a sentence with `;` instead of `.` (not uncommon for pogrammers ;P ), which just seems like a comma after `"`. Did not know you could just comma-separate in a `COUNT(DISTINCT ..))` though, hence the kludgy `CONCAT()` in my answer, always nice to learn something new.
Wrikken
A quick question so I can walk away from this learning something -- the COUNT(DISTINCT email, ip_address) will count a record where the combination of the email AND ip are unique? In my case unique to the comment_id since it's grouped, right?
86Stang
@86Stang: yes=> http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count-distinct (and see the reason why I used `CONCAT()` ;) )
Wrikken
@Wrikken: that's why I prefixed the set with the number of elements in my comment!
Quassnoi
@86Stang: but note that this query won't count records with `NULL` in either of the fields (`ip_address` *or* `email`). Not sure if it's a problem for you.
Quassnoi
@Quassnoi: yes, I'm just trying to find the lamest excuses for an obvious failing to read properly here ;P
Wrikken
+1  A: 
SELECT 
     comment_id,
     COUNT(DISTINCT CONCAT(email,'|',ip_address))
FROM  log
GROUP BY comment_id

As OMG Ponies said, adding a unique in email+ip_address would help, possibly with this to prevent lost notes:

INSERT INTO log (...) VALUES (...) ON DUPLICATE KEY note = CONCAT(note,' ',VALUES(note));
Wrikken