Hi!
I'm trying to create my own little community for learning purposes and I'm having some trouble with getting the data I want from the table. I'm not that high on SQL but I know the basics but when you get into the more complex mysql-lines I get lost.
The problem I'm facing is that I have a table with 3 columns: id1, id2 and confirmed. I read about some techniques for sorting a user's friends in this thread. I decided to use the second method described, meaning that for every friend added you create 2 rows in the table:
+--------+--------+--------+
| id1 | id2 | conf...|
+--------+--------+--------+
| user | friend | 1 |
+--------+--------+--------+
| friend | user | 0 |
+--------+--------+--------+
When a user adds a friend, one row is created showing that the user is a friend of the other user and another row is created showing that friend is now a friend of the user who added the friend. But the latter row shows that the other user hasn't accepted the friend request yet. That is shown in the confirmed column: 1 equals an accepted request and 0 a not yet accepted friend request.
With this information I want to be able to retrieve 3 different arrays of data with PHP. First, I want to be able to find out who the user has confirmed friend relationships with:
+--------+--------+--------+
| id1 | id2 | conf...|
+--------+--------+--------+
| user | friend | 1 |
+--------+--------+--------+
| friend | user | 1 |
+--------+--------+--------+
Secondly, I want to be able to find out who the user has pending requests with:
+--------+--------+--------+
| id1 | id2 | conf...|
+--------+--------+--------+
| user | friend | 1 |
+--------+--------+--------+
| friend | user | 0 |
+--------+--------+--------+
Thirdly, I want to be able to list the requests made to the user by other users which the user himself needs to accept:
+--------+--------+--------+
| id1 | id2 | conf...|
+--------+--------+--------+
| user | friend | 0 |
+--------+--------+--------+
| friend | user | 1 |
+--------+--------+--------+
I've tried something like this to retrieve the first described data:
(SELECT id2 FROM friends WHERE id1='$user' AND confirmed=1)
UNION
(SELECT id1 FROM friends WHERE id2='$user' AND confirmed=1)
But for some reason I still receive the ones that still hasn't accepted the requests and I thought that the following code would only bring the pending friend requests but I still receive the same result as in the previous code:
(SELECT id2 FROM friends WHERE id1='$user' AND confirmed=1)
UNION
(SELECT id1 FROM friends WHERE id2='$user' AND confirmed=0)
So I was hoping someone could help me here to get the correct SQL-syntax since I'm obviously doing something terribly wrong :) Thanks for your help!
P.S. Later I want to add the option to block a user's friend requests by having confirmed set as 2 or something like that. If you would also help me with that it would be greatly appreciated (then of course I would name the "confirmed"-column to something more appropriate, like "status" or something).