0

I have a mail table with four columns: id, userto, userfrom, and message. I want to receive a list of users that have either sent mail to user "example", and I want to join it with a list of users that user "example" sent mail to. However, I don't want any repeating usernames in this list.

How would I do this? I'm sure it has something to do with SELECT DISTINCT and INNER JOIN, but I'm not sure how I would go about doing it.

1 Answer 1

1

I would go with a UNION. Your mileage may vary depending on your RDBMS.

SELECT userto AS listuser FROM mail WHERE userfrom = 'example' UNION SELECT userfrom AS listuser FROM mail WHERE userto = 'example'; 

Edit: You don't need to use SELECT DISTINCT there, UNION will get rid of repetition from within each subquery as well as between the queries.

Sign up to request clarification or add additional context in comments.

1 Comment

it does get rid of reapting values

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.