title: SQL find records based on two columns
body: <p>Given a users table with primary key of <code>id</code>, I have a foreign table called <code>friends</code> which only has two fields (<code>userid1, userid2</code>). This allows us to create any kind of relationship between different users (one to many, one to one, etc). A user can appear in either column and both columns are equal. IOW, a single entry per relationship.</p>
  
  <p>How can I pull all of the friends that a given user <code>id</code> has. Say Jonny, has 3 friends and his user <code>id</code> is <code>16</code>... should my sql query look like this? </p>
  
  <pre><code>SELECT * 
  FROM   db.users 
         JOIN db.friends 
           ON db.users.id = db.friends.userid1 
              AND db.users.id = 16 
  </code></pre>
  
  <p>Hopefully, this is clear. Also, if possible, can I exclude Jonny from the result set?</p>
  
  <p>This query, as listed gies me the following:</p>
  
  <pre><code>id      name    uuid                       birthday        userid1  userid2
  16  jonny   ABCDEFGHIJKLMNOP    1967-04-27 01:00:00     1         2
  16  jonny   ABCDEFGHIJKLMNOP    1967-04-27 01:00:00     1         3
  </code></pre>
  
  <p>This is pretty close, except I want his friends, not jonny</p>
  
  <hr>
  
  <p>Thanks guys, so I got it to work thanks to you. Here is the final working query.</p>
  
  <pre><code>SELECT * 
  FROM db.users
  WHERE db.users.id IN
  (
    SELECT db.friends.userid2 as id FROM db.friends WHERE db.friends.userid1 = 16
      union
    SELECT db.friends.userid1 as id FROM db.friends WHERE db.friends.userid2 = 16
  )
  </code></pre>
  
  <p>which gives me:</p>
  
  <pre><code>id      name    uuid                       birthday 
  2   robin   ABCDEFGHIJKLMNOP    1967-04-27 01:00:00
  3   gary    ABCDEFGHIJKLMNOP    1967-04-27 01:00:00
  </code></pre>