Jump to content

Avoiding duplicates


chomedey

Recommended Posts

Hi,

 

I'm trying to set up a "Notify me when someone comments on a blog entry I have commented on" script, similar to what you get in facebook.

 

I have a comments table, where the username, comment, and blog entry number are entered whenever someone comments.  And a preferences table, which I have joined with the comments table.

 

The only problem is that when I pull all the usernames from the comments table of users who have commented on a particular blog entry, to send them a notification, a user who has commented ten times on a particular blog entry gets ten notifications.  You see the problem?

 

Is there any way of telling mysql (or by using php) to only send one notification per username? (LIMIT 1 won't work, obviously, as there may be more than one user who has commented on a particular blog entry.  I need something like a LIMIT DISTINCT usernames).

 

Any help much appreciated.

 

Julian

Link to comment
https://forums.phpfreaks.com/topic/196660-avoiding-duplicates/
Share on other sites

SELECT DISTINCT looks for distinct rows.  You need to select only the fields that you need AND are distinct:

 

SELECT DISTINCT userid, blogid FROM ...

 

or if you need other data, say how many comments the user has made on the blog, you can use GROUP BY:

 

SELECT userid, blogid, COUNT(commentid) FROM ... GROUP BY userid, blogid

 

which is distinct by virtue of the GROUP BY phrase

 

 

Link to comment
https://forums.phpfreaks.com/topic/196660-avoiding-duplicates/#findComment-1032519
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.