我有2张桌子:

表-用户

userId ||  username ||  profileImage
------------------------------------
1      ||  aa       ||  aaaaa.png

2      ||  bb       ||  bbbb.png

3      ||  cc       ||  cccc.png

4      ||  dd       ||  dddd.png


表-关系

relationshipId  ||  user1  ||  user2 ||  status
-----------------------------------------------
1               ||  1      ||  2    ||  0

2               ||  1      ||  3    ||  1

3               ||  1      ||  4    ||  1

4               ||  2      ||  4    ||  1


因此,我希望1发送了请求并且状态被接受为1的所有用户(3,4)的详细信息。

我尝试过的东西:

$stmt = $this->db->prepare("
SELECT users.uId
     , users.username
     , users.profilePic
  from users
  JOIN relationship
 WHERE (relationship.user_one_Id = :profileUserId || relationship.user_two_Id=:profileUserId)
   AND relationship.statusCode=:statusCode
   AND users.uId != :profileUserId
 LIMIT 6
");

$stmt->bindvalue(":statusCode", 1);
$stmt->bindparam(":profileUserId", $profileUserId);
$stmt->execute();


但是这个查询给了我包括我也不是朋友的行。所以我哪里出错了。

最佳答案

我不知道relationship.user_one_Id出现在图片中的位置。我想你打算写relationship.user1吗?我还假定发件人存储在user1中。在这种情况下,您的查询过于复杂:

SELECT t.uId, t.username, t.profilePic
FROM users t
INNER JOIN relationship r
    ON (t.id = r.user2
        AND r.user1 = :profileUserId
        AND r.status = :statusCode
    )

10-08 13:27
查看更多