简体   繁体   English

SQL关注者和关注者

[英]SQL Following and Followers

Ok I have two tables, one named account_members and another called account_follows . 好的,我有两个表,一个名为account_members ,另一个名为account_follows I want a Twitter style following system where account_members can follow each other. 我想要一个Twitter风格的关注系统,其中account_members可以彼此关注。

Account Follows Table Structure:

id
account_name
followed_name
time

Account Members Table Structure:

id
Account_name
status (Account active or not)

I thought I could get away with just one simple query to get all the accounts being followed: 我以为我可以只用一个简单的查询就可以跟踪所有帐户:

public function following($account_name)
{
    $sql = "SELECT 

    F.id, F.account_name, F.followed_name, F.time, 
    M.account_name AS session_name, M.status 

    FROM account_follows F
    LEFT JOIN account_members M ON F.account_name = M.account_name

    WHERE F.account_name = :account_name 
    AND M.account_name = :account_name

    ORDER BY id DESC LIMIT 5";
}

This will display all the account_members which are being followed (the $account_name is set via the url) 这将显示所有正在遵循的$account_name (通过网址设置$account_name

The issue that I have is allowing the logged in account_member to be able to Follow or Unfollow friends of friends who they are following. 我遇到的问题是允许登录的account_member能够关注或取消关注他们关注的朋友的朋友。 I do a simple check for the logged in account_member to unfollow anyone on their list by doing the following: 我通过以下操作对登录的account_member进行简单检查,以取消关注其列表中的任何人:

if($_SESSION['account_name'] == $row['account_name'])
{
    echo'<a href="" id="..." class="...">Unfollow</a>';
}

The above works fine, but I want to do something similar with the logged in accounts followers followers... If that makes sense? 上面的方法工作正常,但是我想对已登录的帐户关注者执行以下类似操作...如果有意义?

So Bob is logged in, and Bob looks at his following list and clicks on mike and views who mike is following, and from this list has the ability to follow/unfollow people mike is following (and some of which Bob could be following) 这样, 鲍勃(Bob )登录了, 鲍勃(Bob)查看了他的关注列表,然后单击迈克(Mike)并查看迈克正在关注的人,并且该列表具有跟踪/取消关注迈克Mike)正在关注的人的能力(其中有一些鲍勃可以关注)

Any help or guidance is appreciated. 任何帮助或指导表示赞赏。

The query you have will work for any member's account name passed in, but the query itself does not take into account the currently logged in member's follows, so you need to join in their data to it. 您具有的查询将适用于传入的任何成员的帐户名,但是该查询本身未考虑当前登录的成员的关注,因此您需要将其数据加入其中。

The query returns a list of members that the url specified account is following. 该查询返回url指定帐户所遵循的成员列表。 With that is a bit that tells whether the logged in user is also following that member. 这样可以告诉已登录的用户是否也在关注该成员。 Use that bit to decided whether you need to echo a follow or unfollow link. 使用该位来确定您是否需要回显关注链接或取消关注链接。

SELECT 
        theirFollows.id, theirFollows.account_name, 
        theirFollows.followed_name, theirFollows.time, 
        M.account_name AS member_name, M.status, 
        case 
            when myFollows.followed_name is null then 0
            else 1
        end as sessionMemberIsFollowing
FROM    account_members M
        LEFT JOIN account_follows theirFollows
          ON theirFollows.account_name = M.account_name
        LEFT JOIN 
            (
                select followed_name
                from account_follows 
                where account_name = :session_account_name
            ) myFollows
            on myFollows.followed_name = theirFollows.followed_name

WHERE   M.account_name = :account_name

One of your select columns was labled session_name, but that's a bit misleading since the account_name passed in comes from the url. 您选择的列之一已标记为session_name,但这有点误导,因为传入的account_name来自url。 Also, only one of you where clauses is needed since that is column you are joining on. 另外,您中只有一个where子句是必需的,因为那是您要加入的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM