doukanzhuo4297 2016-12-28 22:19
浏览 45
已采纳

如何查询关注的用户帖子?

I am trying to get the posts from the users the current user is following. And also the post owners username and avatar. My tables look like this

Users table

User Id | Username | Avatar

Posts Table

post_id | user_id | body | image

Follows Table

leaders_id | followers_id

Here i s my current query

query = "SELECT *
            FROM posts
            WHERE $user_id IN(
                SELECT leader_id 
                FROM follows 
                WHERE follower_id=$user_id)
            ORDER BY date DESC
            LIMIT 10";
  • 写回答

2条回答 默认 最新

  • dps43633 2016-12-28 22:26
    关注

    Are you looking for something like as follows?

    SELECT p.* FROM posts AS p, follows AS f 
    WHERE p.user_id=f.leader_id AND f.followers_id=$user_id
    

    If you want the leaders' information, you may join the users table.

    SELECT p.*, u.* FROM posts AS p, follows AS f, users AS u 
    WHERE p.user_id=f.leader_id AND p.user_id=u.user_id AND f.followers_id=$user_id
    

    If you want the follower's information, I would recommend you to use a separate query to users table, since the information would repeat in the query results otherwise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致