So, so say you wanted to retrieve the friends of a user with id 1. Obviously if you were to use something like:
Code:
SELECT recipient FROM `friends` WHERE `user_id` = 1 AND STATUS = 1;
You would get a list of user Ids of all of his accepted friends. However, say you wanted a list of their user IDs, and also their name. It may be tempting to use PHP to iterate through that list of user ids, and perform separate queries to retrieve those details from the users table. However, thats evidently quite inefficient (just 30 friends will require 30 extra queries). It's simpler to just use a join, which basically joins the two tables (with a foreign key relationship) and retrieves the required information. So something like this:
Code:
SELECT friends.recipient, users.first_name, users.last_name FROM `friends` LEFT JOINT `users` ON friends.recipient = users.user_id WHERE friends.user_id = 1 AND friends.status = 1
That's just a single query, but will return their friends user id, first and last name.