Reputation: 388
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.
Upvotes: 0
Views: 43
Reputation: 3721
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.
Upvotes: 1