简体   繁体   English

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

[英]How to get posts from people I follow and my posts in php and mysql?

I have table 我有桌子

  1. answers(aid,user_id,...) - posts table 答案(aid,user_id,...)-帖子表

  2. users (userid,name,...) 用户(用户ID,名称,...)

  3. followers(user_one,user_two) - where user_one is the follower and user_two is the followed by person. followers(user_one,user_two)-其中user_one是关注者,user_two是关注者。

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. 我需要显示我关注的人的帖子以及我的家庭供稿中使用php和mysql的帖子。但是目前,它无法正常工作。 Each posts is showing 7 times. 每个帖子显示7次。

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";  
?>

Didn't see you also want to return your posts. 没看到您还想退回帖子。 Try below sql: 试试下面的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". 您的sql中有错误:SELECT * FROM左边的答案在users.userid = answers.user_id上加入用户。LEFT在followers上加入关注者。user_one= answers.user_id WHERE答案

the second left join should be followers.user_two=ansers.user_id, and the where statement should be followers.user_one='$my_id'; 第二个左联接应该是followers.user_two = ansers.user_id,而where语句应该是followers.user_one ='$ my_id';

One option here is to phrase your query as a union of two queries. 这里的一种选择是将您的查询表述为两个查询的并集。 The first half of the union below finds all posts given by people you follow. 以下联盟的前半部分查找您关注的人发布的所有帖子。 The second half of the union finds all of your posts. 工会的后半部分查找您的所有职位。

SELECT a.*
FROM answers a
INNER JOIN followers f
    ON (a.user_id = f.user_two AND f.user_one = $my_id)
UNION ALL
SELECT a.* FROM answers WHERE user_id = $my_id;

I'm not a PHP person, but hopefully you can easily adapt the above query into your code. 我不是PHP人士,但希望您可以轻松地将上述查询改编成您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM