title: Grouping SQL results from a Union
body: <p>I have a table in my database used for users to follow eachother.</p>
  <p>The table goes as follows:</p>
  <pre><code>UniqueID
  FollowerID
  FollowedUserID
  </code></pre>
  <p>FollowID is the UserID of the person who is following someone else.</p>
  <p>FollowedUserID is the UserID of the person who is being followed by the FollowID User.</p>
  <p>I'd like to retrieve a list of connections from this table based off a single user. My query should return all the other UserIDs that the current user is <strong>either following or being followed by</strong> with no overlaps.</p>
  <p>So say we have a few entries here:</p>
  <pre><code>FollowerID   FollowedUserID
      1              2
      1              3
      4              1
      2              1
  </code></pre>
  <p>This would show that the User <strong>1</strong> is following users 2 and 3, and thus users 2 and 3 are being followed by user <strong>1</strong>. On the other side of the spectrum it shows that User <strong>1</strong> is followed by users 2 and 3.</p>
  <p>What I'd like to do is find out the connections that user <strong>1</strong> has, so the query should return users: 2, 3, and 4 (user <strong>1</strong> is following users 2 and 3 and is being followed by user 4)</p>
  <p>How can I achieve this from a single query?</p>
  <p>A <strong>connection</strong> is considered either being followed or following someone else, so it is possible that some duplicate results can occur (if both users are following eachother). I'd like to be able to group these results so that the result is distinct.</p>
  <p>I've tried a UNION query similar to:</p>
  <pre><code>SELECT FollowerID, FollowedUserID From Follows WHERE FollowerID = 1
  UNION
  SELECT FollowerID, FollowedUserID FROM Follows WHERE FollowedUserID = 1
  </code></pre>
  <p>Theoretically I'd like to Group FollowerID and FollowedUserID together to keep them distinct.</p>
  <p>I'm not interested in retrieving a non-distinct result and then creating a dataset of unique results php side -- the query should return distinct values only.</p>