title: Select all ids for of the friends for a particular user
body: <p>Need some assistance from SQL experts out there.   Quick overview of the structure of my two tables that I'm looking to query from.  </p>
  
  <ul>
  <li><p>users table</p>
  
  <ul>
  <li>id - (int) primary key</li>
  <li>name - (string)</li>
  </ul></li>
  <li><p>friends table</p>
  
  <ul>
  <li>id - (int) primary key</li>
  <li>toid - int</li>
  <li>fromid - int</li>
  </ul></li>
  </ul>
  
  <p>friends table holds the friendship relationship between users.  The tricky part is that it holds two records per friendship.  </p>
  
  <p>So for example if we have three users in the database with id 1, 2, 3 and their names are Rob, John and Adam respectively.  </p>
  
  <p>If Rob and John are friends, the friends table has the following two entries:</p>
  
  <p>id  toid   fromid</p>
  
  <p>1 &nbsp; 1&nbsp;&nbsp; 2</p>
  
  <p>2 &nbsp; 2&nbsp;&nbsp; 1</p>
  
  <p>There are also situations where there is only one entry in the friends table.  This happens when a request has been sent by one person but not excepted by the recipient yet.  </p>
  
  <p>What I am trying to accomplish is to select all id and names for of the friends for a particular user.  (Friends being the ones that have both entries in the friends table, not just one).</p>
  
  <p>What I have tried so far gives me list of ids for both friends and users who have either sent or received a request for a particular user.  Below is the closest I've gotten.</p>
  
  <blockquote>
    <p>SELECT DISTINCT <br>
      users.ID userid <br>
      users.name name <br>
    FROM users <br>
    INNER JOIN friends <br>
    ON users.id = friends.fromid OR users.id = friends.toid <br>
    WHERE (friends.fromid = 1 OR friends.toid = 1) AND users.ID != 1 <br></p>
  </blockquote>