A typical friendship in a friend list occupies 2 rows in a table.
Entry number 1, which tells us user 1 is friends with user 2
Entry number 2, which tells us user 2 is friends with user 1
A slightly, large, query can then tell is if they are mutual friends. And, it takes up 2 units of space, per friendship.
Example 2)
This table is a little more complex, but easy to understand. Since we cant name columns the same thing we've chosen to have the user who attempted to make the friendship first "USER" and the person they want to be friends with is "FRIEND".
In this case, user 1 is trying to become friends with user 2.
When user 2 accepts the friendship, all that happens is the column is_friend gets changed to 1.
To figure out if two users are friends, its a very simple logic query.
SELECT is_friend FROM friendship
where (user = 1 or friend = 1) and (user = 2 or friend = 2)
You can then return the status of the friendship. And, even tho were only using ONE row we can still clearly identify who added whom first.
(note: only downside to this method is you can't allow users to add them-self as a friend.)
Code:
id | user | friend
----------------
1 | 1 | 2
2 | 2 | 1
Entry number 1, which tells us user 1 is friends with user 2
Entry number 2, which tells us user 2 is friends with user 1
A slightly, large, query can then tell is if they are mutual friends. And, it takes up 2 units of space, per friendship.
Example 2)
Code:
id | user | friend | is_friend
------------------------------
1 | 1 | 2 | 0
This table is a little more complex, but easy to understand. Since we cant name columns the same thing we've chosen to have the user who attempted to make the friendship first "USER" and the person they want to be friends with is "FRIEND".
In this case, user 1 is trying to become friends with user 2.
When user 2 accepts the friendship, all that happens is the column is_friend gets changed to 1.
To figure out if two users are friends, its a very simple logic query.
SELECT is_friend FROM friendship
where (user = 1 or friend = 1) and (user = 2 or friend = 2)
You can then return the status of the friendship. And, even tho were only using ONE row we can still clearly identify who added whom first.
(note: only downside to this method is you can't allow users to add them-self as a friend.)