Join tables BUT avoid duplicates

Greetings,

as a MySql newbie I need your help… again…
What I want is to select all the users’ email addresses from a table ‘users’ if they have submitted a comment to a page. And I was thinking about this query :


SELECT users.user_email, users.user_name, comments.* 
FROM users, comments
WHERE users.user_name = comments.sender_uname AND comments.page = '$page' AND comments.page_id = '$page_id'
ORDER BY comments.id ASC

BUT, if a user submitted more than one comment, an email notification will be sent more than once (as many times as the number of the user’s comments). How can I build a query to select a user’s email address just ONCE from the table ‘users’ if he submitted a comment?

Your help is highly appreciated! :slight_smile:
Regards

bofadem :wink: :wink:

Thank you man! :slight_smile:


SELECT DISTINCT
    users.user_email
  , users.user_name
FROM users
INNER JOIN comments
ON users.user_name = comments.sender_uname 
AND comments.page = '$page' 
AND comments.page_id = '$page_id'


select user_email, 
       user_name 
  from users
 where exists
     (select *
        from comments 
       where users.user_name = comments.sender_uname 
         and comments.page = '$page' 
         and comments.page_id = '$page_id')