在我名为Follow 的table 中,有被关注者的信息。例如,假设我的用户 ID 为 1:
tableID | followerID | followedID
___________________________________
1 | 1 | 13
2 | 1 | 32
在这个 table 中,我关注具有 13 和 32 个用户 ID 的用户。
我有一个名为 table 的故事。在这个 table 中,有每个用户写的博客文章。现在我想在主页上查看我关注的用户的博客。
storyID | userID | storyDesc
___________________________________
1 | 1 | Lorem Ipsum
2 | 13 | Dolor set
3 | 32 | lorem ipsum dolor sit amet
通常我可以这样做:
$connect=$db->preparea("SELECT * FROM story");
$connect->execute();
while($story = $connect->fetch(PDO::FETCH_ASSOC){
$con=$db->prepare("SELECT * FROM follow where followerID='1' and
followedID='{$story['userID']}'");
$con->execute();
$count = $con->rowCount();
if($count == 1){
echo $story['storyDesc'];
}
}
我可以直接从 sql 查询中执行此操作吗?
回答1
我不知道这是否是正确的做法,但我尝试了这样的事情并且它奏效了:
$connect=$db->prepare("SELECT * FROM story RIGHT JOIN follow ON story.userID=follow.followedID where follow.followerID='{1}'");
$connect->execute();
while($result = $connect->fetch(PDO::FETCH_ASSOC)){
echo $result['storyDesc'];
}
感谢您的帮助:)