首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL跟随和跟随

SQL跟随和跟随
EN

Stack Overflow用户
提问于 2013-03-19 09:23:28
回答 1查看 3.4K关注 0票数 2

好的,我有两个表,一个叫account_members,另一个叫account_follows。我想要一个推特风格的跟踪系统,account_members可以互相跟踪。

代码语言:javascript
复制
Account Follows Table Structure:

id
account_name
followed_name
time

Account Members Table Structure:

id
Account_name
status (Account active or not)

我想我只需要一个简单的查询就可以让所有的帐户都被跟踪:

代码语言:javascript
复制
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";
}

这将显示所遵循的所有account_members ( $account_name是通过url设置的)

我遇到的问题是允许登录的account_member能够跟踪或取消跟踪他们所跟踪的朋友的朋友。通过执行以下操作,我对登录的account_member进行简单检查,以取消列表中任何人的跟踪:

代码语言:javascript
复制
if($_SESSION['account_name'] == $row['account_name'])
{
    echo'<a href="" id="..." class="...">Unfollow</a>';
}

上面的工作很好,但我想做一些类似的登录帐户追随者.如果有道理的话?

因此, Bob 登录,Bob查看他的列表并单击mike并查看mike所关注的内容,并且从这个列表中可以跟踪/取消跟踪以下人员:mikeE 215是这样的(鲍勃可能会跟随其中一些)。

如有任何帮助或指导,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-19 13:27:51

您所拥有的查询将适用于传入的任何成员的帐户名称,但查询本身不考虑当前登录成员的以下内容,因此您需要将它们的数据加入到其中。

查询返回url指定帐户所遵循的成员列表。使用它可以告诉登录用户是否也在跟踪该成员。使用该位来决定您是否需要回显跟踪或取消跟踪链接。

代码语言:javascript
复制
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

您的选择列之一被标记为session_name,但这有点误导,因为传入的account_name来自url。另外,你们中只有一个需要从句,因为这是您要加入的列。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15495570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档