简体   繁体   English

SQL:从关注者中选择所有帖子

[英]SQL : Select all Posts from Followers

Im trying to find an efficient way to display all posts from people who are being followed by the logged in account holder. 我试图找到一种有效的方法来显示登录帐户持有者所关注人员的所有帖子。

There are two key tables: 有两个关键表:

1- Posts 1个帖子

table name : posts 表名: posts

id , account_name , published, body idaccount_namepublished, body

2- Follows 2-跟随

Table name : follows 表名: follows

id , account_name , followed_name idaccount_namefollowed_name

I'm trying to find a way that i can display all the posts from all the accounts that are being followed. 我正在尝试找到一种方法,可以显示来自所有正在遵循的帐户的所有帖子。 The connection between Posts and Follows is the Account_name . Posts和Follows之间的联系是Account_name

I understand that it will probably be a join, but it's how I construct the WHERE clause. 我知道这可能是一个联接,但这就是我构造WHERE子句的方式。 So far I have the following (The account name is set via $_SESSION['account_name']): 到目前为止,我有以下内容(帐户名称是通过$ _SESSION ['account_name']设置的):

$sql = "SELECT * FROM posts LEFT JOIN follows ON posts.account_name = follows.account_name WHERE  --- How would I only get the posts from the accounts being followed ?---"

I'm sure this is something simple my brain just feels drained and I cant seem to work it out. 我敢肯定,这很简单,我的大脑只是感到筋疲力尽,我似乎无法解决。

UPDATE Attempting in PDO 在PDO中尝试更新

Returning NULL at the moment, 此刻返回NULL,

$sql = "SELECT * FROM share_posts WHERE account_name IN (SELECT followed_name FROM $this->account_follows WHERE account_name = :account_name)";
    return $this->AC->Database->select($sql, array('account_name' => $account_name)); 

The goes to my Database Class: 转到我的数据库类:

    public function select($sql, $array = array(), $fetch_mode = PDO::FETCH_ASSOC)
    {

        $stmt = $this->AC->PDO->prepare($sql);

        foreach ($array as $key => $value) 
        {
            $stmt->bindValue("$key", $value);
        }

        $stmt->execute();

        return $stmt->fetchALL($fetch_mode);

    }

The returned data is NULL at the moment even though the logged in account has followed other accounts. 即使已登录的帐户已跟随其他帐户,此刻返回的数据仍为NULL。

$account = $_SESSION['account_name'];

//do some sql injection checking on $account here

$sql = "SELECT * FROM posts WHERE account_name IN (SELECT followed_name FROM follows WHERE account_name='".$account."')";

This will get all the posts where the account name matches somebody you follow. 这将获得与帐户名称匹配的所有帖子。 I wasnt sure who was following who, but in this case the followed_name are the people account_name is following. 我不确定谁在追踪谁,但是在这种情况下,followed_name是人物account_name在追踪。 If thats the other way around, switch the values 如果那是相反的方式,请切换值

$sql = "SELECT * FROM posts WHERE account_name IN (SELECT account_name FROM follows WHERE followed_name='".$account."')";

I will write this the way I interpret your question. 我将以此方式解释您的问题。

What you need to do is select only the posts from the users that are followed by your logged in user. 您需要做的是仅选择用户中的帖子,然后再选择已登录用户。

To break this down, first you want to select the users followed by the logged in user. 要对此进行分类,首先要选择用户,然后选择登录的用户。 To do this, we use the Follows table. 为此,我们使用“关注”表。

We then want to select the posts by these users. 然后,我们要选择这些用户的帖子。 As such my query would be this. 因此,我的查询将是这样。

SELECT posts.* FROM follows
    LEFT JOIN posts ON posts.account_name = follows.follows_name
    WHERE follows.account_name = $logged_in_user

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

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