douou9094747 2014-07-30 20:48
浏览 55
已采纳

如何在一个查询中获取以下用户内容和您自己的内容?

I'm trying to make a social network; I have a table for users' posts. The table looks like:

id fromuid touid date ip content

fromuid is the user who posted the post, and touid is set to whoever you post to. So if you go on someone's page and make a post, touid is set to your UID. However, if you're home and don't post at any one touid will be set to your UID.

Now here's my problem: I'm trying to get the content of the users you're following. There's a table like so for followers:

id follower following date

Then, in my post's class, I get all the followers and put them into a predefined array like so:

        $getRelatedPostsQuery = new Database(); 
    $getFollowers = $getRelatedPostsQuery->query(array("SELECT * FROM follower WHERE follower = ?", array($this->uid)))->fetchAll(PDO::FETCH_ASSOC);
    foreach ($getFollowers as $key) {
        $followers[] = $key['following'];
    }

This is where I begin to struggle. I have this query here:

    $getRelatedPosts = new Database();
    $relatedPostsResult = $getRelatedPosts->query(array("SELECT * FROM posts WHERE fromuid IN ('". implode("', '", $followers) ."') ORDER BY id DESC", array()))->fetchAll(PDO::FETCH_ASSOC);

This works absolutely fine, but the problem is, if for example I follow a user called Jack, and a user I'm not following, say, Jill, posts on Jack's wall, I'll see what Jill posted. I obviously want to only see the content of the users I have followed. How would I best go about doing this? Also, I'm guessing this is probably an over-complicated way of doing this, yes? Any suggestions would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dongtan6543 2014-07-30 21:03
    关注

    If I'm understanding you correctly, you're wanting all the posts from only those people whom you're following, and not posts by other people to those that you're following.

    If that's the case, a simple addition to your where clause should do the trick:

    $relatedPostsResult = $getRelatedPosts->query(array("SELECT * FROM posts WHERE fromuid IN ('". implode("', '", $followers) ."') AND fromuid = touid ORDER BY id DESC", array()))->fetchAll(PDO::FETCH_ASSOC);
    

    This will tell the query to only grab the posts where fromuid is the same as touid (posts where the person posted on their own "wall").

    You also asked if there is a simpler way to do it, and there is if you combine the queries:

    $getRelatedPosts = new Database();
    $relatedPostsResult = $getRelatedPosts->query(
                                                  array("SELECT * FROM posts WHERE fromuid IN(SELECT fromuid FROM follower WHERE follower = ?) AND fromuid = touid ORDER BY id DESC",
                                                          array($this->uid))
                                                        )->fetchAll(PDO::FETCH_ASSOC);
    

    Or if you'd rather use a join:

    $getRelatedPosts = new Database();
    $relatedPostsResult = $getRelatedPosts->query(
                                                  array("SELECT * FROM posts, follower WHERE follower.follower = posts.fromuid AND follower.follower = ? AND follower.fromuid = follower.touid ORDER BY posts.id DESC",
                                                          array($this->uid))
                                                        )->fetchAll(PDO::FETCH_ASSOC);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧