I have two tables:
users
:
- user_id
- user_name
- ...
friends
:
- user_id (user who sent friend request)
- friend_id (user who received a friend request)
- confirmed (1 = friends, 0 = friend request)
I want each user to be able to see their friends and friend requests. I have problem with displaying friends usernames. I made it work, but it's a poor solution and I want to solve this problem using only one query (if possible).
I tried:
$sql = "SELECT friends.friend_id, friends.user_id, users.user_name FROM friends
INNER JOIN users ON friends.user_id = users.user_id
WHERE friends.user_id = " . $_SESSION['user_id'] . " AND confirmed = 1
OR friends.friend_id = " . $_SESSION['user_id'] . " AND confirmed = 1;";
My problem is that it will show user_name
of users that have user_id
same as user_id
in friends table. My query needs to check if user_id
is the same as the $_SESSION['user_id']
, and if it's the same then it must return user_name
of the user that has the user_id
the same as friend_id
in friends
table.