You want something like this:
SELECT p.name, f.friend_id FROM person AS p INNER JOIN friends AS f ON p.person_id = f.person_id WHERE p.person_id = 1
This joins two tables together using p.person_id = f.person_id
If a person has no friends, you will not get any rows back - if you do not want this, use LEFT JOIN and you will get one row with NULL friend_id .
Edit: if you want to join friends again:
SELECT p.name AS person_name, friend.name AS friend_name FROM person AS p
You may need a 3-way connection similar to this for your application, but as a rule, you only need 2 paths as above, or like this:
SELECT p.name, f.friend_id FROM person AS p INNER JOIN friends AS f ON p.person_id = f.friend_id WHERE f.person_id = 1
This will give you the names of all people who are familiar with person_id 1 (but not with person_id 1)
source share