SQL to get Unique Email

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

SQL to get Unique Email

Post by ninethousandfeet »

hello,
i allow my users to enter conversations with one another. if a new comment is posted in one of these conversations, then i would like all users involved to receive an email to let them know about the new comment. can i use SQL to query only the unique email addresses in a conversation? i tried LIMIT and that didn't work.
the way it stands now, users are sent an multiple emails (i only want each participating user to receive one email)...

SELECT testComment.email, testComment.comment_title FROM testComment WHERE testComment.comment_title = %s GROUP BY testComment.email

thank you!
sujithtomy
Forum Commoner
Posts: 46
Joined: Tue Mar 24, 2009 4:43 am

Re: SQL to get Unique Email

Post by sujithtomy »

Hello,

Use DISTINCT keyword before fieldname

eg:

Code: Select all

SELECT DISTINCT email FROM table_name WHERE condition
Good Luck :)

Regards,
Sujith
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: SQL to get Unique Email

Post by jayshields »

Please don't put links to your blog behind keywords.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: SQL to get Unique Email

Post by ninethousandfeet »

i tried using DISTINCT and it had no affect on my results... can you think of any other way to do it?
sujithtomy
Forum Commoner
Posts: 46
Joined: Tue Mar 24, 2009 4:43 am

Re: SQL to get Unique Email

Post by sujithtomy »

Hello,

I am not sure this way is correct, but unless you find a good answer you can try as follows,

get all email-ids to an array (php), use array_unique() —which Removes duplicate values from an array,
then pass result to mailing program.

Good Luck!! :)

Regards,
Sujith
Post Reply