Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to query names of friends from a friend's table
    primarykey
    data
    text
    <p>Given the following table, I'm trying to write a query that will return the names of user 104's friends.</p> <p>MYSQL TABLE friends</p> <pre><code>id | requester | requestee | status -------------------------------------- 1 | 140 | 104 | 'friends' 2 | 104 | 151 | 'friends' </code></pre> <p>I tried this but it's not quite right because it returns the name of user 140 (which is correct) and the name of user 104 (which is incorrect - I wanted the name of user 151). I realize part of my problem is due to the "ON f.requester = u.id", but I'm not sure how to write the query to get what I want.</p> <pre><code>SELECT u.name FROM users u INNER JOIN friends f ON f.requester = u.id WHERE ( f.requester =104 AND STATUS = 'friends' ) OR ( f.requestee =104 AND STATUS = 'friends' ) LIMIT 0 , 30 </code></pre> <p>Any help is appreciated. Thanks.</p> <p><strong>UPDATE:</strong> I also tried making a "bigger query" and using PHP to extract what I need. This sort of works but I'm only able to get the user IDs so far. $query = "SELECT * FROM <code>friends</code> WHERE requester='$userid' OR requestee='$userid' AND status='friends'"; $results = mysql_query($query) or die(mysql_error());</p> <pre><code>$count = 0; while ($row = @mysql_fetch_assoc($results)){ $count++; if ( !is_null($row['requester']) ) { $requester = $row['requester']; $requestee = $row['requestee']; if($requester == $userid) echo $requestee . "&lt;br /&gt;&lt;br /&gt;"; else echo $requester . "&lt;br /&gt;&lt;br /&gt;"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload