您好我有两张表PAS_User
和PAS_Follow
,请参阅下图,
我正在努力解决的是MySQL的查询,用户只能看到其他用户的列表,其中关系id是它们之间的倒数。
对于互惠(互相),PAS_Follow
表必须记录,所以我们的用户名为1
,用户ID为2
table将包含两行数据,第1行为:
flow_follower_user_id = 1 && flow_followed_user_id = 2
和第2行
flow_follower_user_id = 2 && flow_followed_user_id = 1
我有这个查询,但它似乎显示没有关注源用户的用户。
SELECT DISTINCT PAS_Follow.folw_followed_user_id AS user_user_id, PAS_User.user_first_name, PAS_User.user_last_name, PAS_User.user_avatar_url, PAS_User.user_sector, PAS_User.user_job_type, PAS_User.user_pass_token FROM PAS_User RIGHT OUTER JOIN PAS_Follow ON PAS_User.user_user_id = PAS_Follow.folw_followed_user_id WHERE `folw_followed_user_id` =**:id** OR `folw_follower_user_id` =**:id** AND `folw_deleted` = 0 ORDER BY PAS_Follow.folw_followed_user_id ASC
此示例中**:id**
= 1。
这适用于直接消息平台,因此当用户启动用户列表时,他们只会看到具有互惠关系的用户,这一点非常重要。
感谢任何帮助,我希望对问题的解释是完整的。
****下面回答****
希望这有助于其他人
SELECT foll_second.folw_follower_user_id AS user_user_id, user.user_first_name, user.user_last_name, user.user_avatar_url, user.user_job_type FROM PAS_Follow foll_first, PAS_Follow foll_second, PAS_User user WHERE foll_first.folw_follower_user_id = foll_second.folw_followed_user_id AND foll_second.folw_follower_user_id = foll_first.folw_followed_user_id AND foll_first.folw_follower_user_id =:id AND foll_second.folw_follower_user_id = USER .user_user_id
答案 0 :(得分:1)
您在用户表中缺少一个ID。如果你有,并且我们称之为id
,你可以使用它:
SELECT *
foll_second.folw_follower_user_id AS user_user_id,
user.user_first_name,
user.user_last_name,
user.user_avatar_url,
user.user_job_type
FROM
PAS_Follow foll_first,
PAS_Follow foll_second,
PAS_User user
WHERE
foll_first.folw_follower_user = foll_second.folw_followed_user
AND foll_second.folw_follower_user = foll_first.folw_followed_user
AND foll_first.folw_follower_user = **:id**
AND foll_second.folw_follower_user = user.id;
这将为您提供两个链接关系的组合列表,以及通过这些关系连接到**:id**
的用户。
从两个跟随关系和用户的所有可能组合中选择与您想要的东西相对应的组合,这意味着: