[英]Select Mutual Followers
I know this has many questions here but no answer I've found works for me. 我知道这里有很多问题,但没有答案,我发现对我有用。 I'm building a social network and I'm working on the user list page.
我正在建立一个社交网络,并且正在用户列表页面上。 In my database are two tables,
users
& followers
. 在我的数据库中有两个表
users
和followers
。 The id
column in the users
table is a foreign key in the followers
table whose columns are id
, user_id
& follower_id
. users
表中的id
列是followers
表中的外键,其user_id
id
, user_id
和follower_id
。 The follower_id
is the user id of the user who followed the user_id
user. follower_id
是关注user_id
用户的用户的用户ID。
When showing the list of all users on the social network I want to exclude: 1)The users being followed by the logged_in_user
2)The users following the logged_in_user
当显示在社交网络上的所有用户列表我要排除:1)被随后的用户
logged_in_user
2)用户以下logged_in_user
When showing the list of all users I also want to prioritize any mutual followers to be displayed first. 当显示所有用户的列表时,我还想优先考虑任何相互关注的人首先显示。
This are my current queries ( $userid
refers to the logged_in_user
and $start
is an integer sent when the user loads more users) 这是我当前的查询(
$userid
是指logged_in_user
而$start
是在用户加载更多用户时发送的整数)
<?
//list of users following the `logged_in_user`
$followers = $db->query(
'SELECT followers.id, followers.user_id, followers.follower_id, users.id, users.username, users.profileimg
FROM followers, users
WHERE followers.user_id=:userid
AND followers.follower_id=users.id
LIMIT 8 OFFSET '.$start.';', array(':userid'=>$userid));
//list of users being followed by the `logged_in_user`
$following = $db->query(
'SELECT followers.id, followers.user_id, followers.follower_id, users.id, users.username, users.profileimg
FROM followers, users
WHERE followers.follower_id=:userid
AND followers.user_id=users.id
LIMIT 8 OFFSET '.$start.';', array(':userid'=>$userid));
//list of all users: here I excluded the `logged_in_user`
$users = $db->query(
'SELECT users.id, users.username, users.profileimg, users.gender, users.last_seen
FROM users
WHERE users.id!=:userid
LIMIT 8 OFFSET '.$start.';', array(':userid'=>$userid));
?>
Could anyone help me tie these three queries together to get the desired result: A list of all users who are following/being followed by the users who are following/being followed by the logged_in_user
ie mutual followers. 谁能帮助我将这三个查询结合在一起以得到所需的结果:正在关注/正在关注的所有用户的列表,正在关注/正在关注的所有用户的列表,然后是
logged_in_user
即相互关注的列表。 Thanks. 谢谢。
UPDATE I have found some code to help me remove the users following/being followed by logged_in_user
更新我找到了一些代码来帮助我删除跟随/之后
logged_in_user
之后的用户
<? //remove followers from list of users foreach ($followers as $fr) { foreach($users as $elementKey => $element) { foreach($element as $valueKey => $value) { if($valueKey == 'username' && $value == $fr['username']){ //delete this particular object from the $users unset($users[$elementKey]); } } } } //remove users following logged_in_user from list of users foreach ($following as $fg) { foreach($users as $elementKey => $element) { foreach($element as $valueKey => $value) { if($valueKey == 'username' && $value == $fg['username']){ //delete this particular object from the $users unset($users[$elementKey]); } } } } ?>
All I need now is to fetch all users whose id
matches the follower_id
column in the followers
table because their corresponding user_id
column will be the mutual followers between the logged_in_user
and the users followed/following him/her. 我现在需要做的就是获取所有
id
与followers
表中的follower_id
列匹配的用户,因为他们对应的user_id
列将是logged_in_user
与关注/关注他/她的用户之间的相互关注者。
Not an answer. 没有答案。 Too long for a comment...
评论太久了...
If it was me, I'd start with something like this (including DDLs of same)... 如果是我,我将从这样的东西开始(包括相同的DDL)...
1 Romeo
2 Juliet
3 Benvolio
4 Mercutio
5 Tybalt
user follows
5 2
4 1
4 2
1 3
...and instead ask, what query can tell us who else is following/being followed by the individuals following/being followed by Romeo. ...而是询问,什么查询可以告诉我们还有谁跟随/正在跟随谁,接着是个人跟随/正在跟随罗密欧。
All I did next after the update above was run another query that fetched the desired users. 在执行以上更新之后,我接下来要做的就是运行另一个获取所需用户的查询。
<? foreach ($users as $ur) { $user_id = $ur['id']; $mutual[]= $db->query('SELECT followers.id, followers.user_id, followers.follower_id, users.id, users.username, users.profileimg FROM followers, users WHERE followers.follower_id=:userid AND followers.user_id=users.id LIMIT 100 OFFSET '.$start.';', array(':userid'=>$user_id)); } echo json_encode($mutual); ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.