我试图找到一种有效的方式来显示登录帐户持有人跟踪的人的所有帖子。SQL:选择所有帖子从关注者
有两个关键表:
1-帖子
表名:posts
id
,account_name
,published, body
2-遵循
表名:follows
id
,account_name
,followed_name
我试图找到,我可以从所有正在遵循的账户显示所有帖子的方式。帖子和关注之间的连接是Account_name
。
我知道它可能是一个连接,但它是我如何构造WHERE
子句。到目前为止,我有以下(帐户名通过$ _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 ?---"
我敢肯定,这是简单的东西我的大脑只是感觉耗尽,我似乎无法工作了。
更新PDO
试图在此刻返回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));
的进入我的数据库类:
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);
}
返回的数据是目前空连尽管登录账户已经跟随其他账户。
你能澄清一下,是不是ACCOUNT_NAME的情况如下followed_name,或者是followed_name如下ACCOUNT_NAME(谁是下谁从该表) – CodePB 2013-03-12 15:29:53
有什么不对您的查询“原样”(无WHERE子句)? – 2013-03-12 15:35:07
@pburgess Account_name是已登录的帐户,而Following_name是正在遵循的帐户...因此,account_name的follow_name为 – HireLee 2013-03-12 15:51:00