0

这是我获取用户朋友的查询。但有一个问题。如果接收者是会话用户,它会将自己作为朋友返回。

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

这次向我发送友谊请求的另一个用户将他/她自己视为他/她的朋友。

4

2 回答 2

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'
于 2013-07-19T14:45:41.870 回答
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"]."
于 2013-07-19T15:47:12.280 回答