这是我获取用户朋友的查询。但有一个问题。如果接收者是会话用户,它将自己作为朋友返回。
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'"
下表是我的MySQL表设计。我以user_id =16
身份登录了网站。然后user_id=19
向我发送了一份请求。然后我批准了。这里的一切都很美好但是当我想看到我的朋友时,我也将自己视为我的朋友。
如何在朋友页面上排除自己?
尝试
如果我更改此行
JOIN friendship ON user.id = friendship.receiver_id
要
JOIN friendship ON user.id = friendship.sender_id
这次向我发送友情请求的另一位用户将自己视为他/她的朋友。
答案 0 :(得分:0)
将
"AND user.id != " . $_SESSION["SES_USER_ID"]
不行吗?所以你的整个查询将是:
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'
AND user.id != " . $_SESSION["SES_USER_ID"]
编辑: 我认为您的查询应该是:
SELECT user.name_surname, user.id, friendship.receiver_id, friendship.sender_id
FROM user
JOIN friendship ON user.id = friendship.receiver_id
AND friendship.receiver_id != " . $_SESSION['SES_USER_ID'] . "
WHERE (friendship.receiver_id =".$_SESSION["SES_USER_ID"]." OR friendship.sender_id =".$_SESSION["SES_USER_ID"] . ")
AND is_approved='1'
答案 1 :(得分:0)
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"]."