doufangyan6862 2017-11-01 05:20
浏览 130
已采纳

如何从我关注的人和php和mysql中的帖子中获取帖子?

I have table

  1. answers(aid,user_id,...) - posts table

  2. users (userid,name,...)

  3. followers(user_one,user_two) - where user_one is the follower and user_two is the followed by person.

I need to show posts from people I follow and my posts using php and mysql in my home feed.But currently it is not working as expected.Each posts is showing 7 times.

curent select query

<?php

    $my_id = $_SESSION['user_id'];
     $sql_query = "SELECT * FROM answers left join users on users.userid = answers.user_id LEFT join followers on followers.user_one = answers.user_id WHERE answers.user_id= '$my_id' ORDER BY answers.date_created DESC";  
?>
  • 写回答

2条回答 默认 最新

  • dsjq62428 2017-11-01 05:32
    关注

    Didn't see you also want to return your posts. Try below sql:

         select * from answers, users, (select user_one user_id from followers where user_two='$my_id' union select '$my_id' user_id) a where answers.user_id=users.user_id and answers.user_id=a.user_id;
    

    There is error in your sql: SELECT * FROM answers left join users on users.userid = answers.user_id LEFT join followers on followers.user_one = answers.user_id WHERE answers.user_id= '$my_id' ORDER BY answers.date_created DESC".

    the second left join should be followers.user_two=ansers.user_id, and the where statement should be followers.user_one='$my_id';

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

报告相同问题?