hakkikonu hakkikonu - 5 months ago 9
MySQL Question

Exclude myself from friendship page - MySQL

This is my query to fetch user's friends. But there is a problem. If receiver is session user it is returning itself as friend.



SELECT user.name_surname, user.id, friendship.receiver_id, friendship.sender_id
FROM user
JOIN friendship ON user.id = friendship.receiver_id
WHERE
(friendship.receiver_id =".$_SESSION["SES_USER_ID"]." OR friendship.sender_id =".$_SESSION["SES_USER_ID"].")
AND is_approved='1'"


Below table is my MySQL table design. I'am logged on as
user_id =16
to site. Then
user_id=19
sent me a frienship request. Then I approved it. Everything is beautiful up here but when I want to see my friends I also see myself as my friend.

How do I exlude myself at friends page?

friendship table




attempted below

If I change this line

JOIN friendship ON user.id = friendship.receiver_id


TO

JOIN friendship ON user.id = friendship.sender_id


this time another user who sent me friendship request sees his/her self as his/her friend.

Answer
SELECT user.name_surname, user.id, friendship.receiver_id, friendship.sender_id
FROM user
JOIN friendship ON user.id = friendship.sender_id OR user.id = friendship.receiver_id
WHERE (friendship.receiver_id =".$_SESSION["SES_USER_ID"]." OR friendship.sender_id  =".$_SESSION["SES_USER_ID"].") 
AND is_approved='1'
AND user.id != ".$_SESSION["SES_USER_ID"]."