简体   繁体   中英

How do I Inner Join users table with followed users posts?

I am trying to retrieve the posts from the people the current user is following along with their details. I've been researching at it seems a join is the right way to do it but I am not sure what is wrong with it.

Here is my code

$queryfeed = "SELECT posts.user_id, posts.body, posts.image, posts.has_image 
FROM posts, follows WHERE posts.user_id =  follows.leader_id 
AND follows.follower_id =$user_id INNER JOIN users ON users.user_id 
= follows.leader_id";

$resultfeed = $db->query($queryfeed);
if($resultfeed->num_rows > 0) {
    while( $rowfeed = $resultfeed->fetch_assoc() ) {       
        if($rowfeed['posts.has_image'] == 1) {
    ?>
            <article class="post">
                <div class="post-head cf">
                    <a class="userpic" href=""><img src="<?php echo $userpic ?>" alt="<?php echo $rowfeed['users.username'] ?>"></a>
                    <a href="" class="username">
                        <?php echo $rowfeed[users.'username']; ?>
                    </a>
                </div>
                <img src="users/user_<?php echo $rowfeed['posts.user_id'] ?>/posts/<?php echo $rowfeed['posts.image']; ?>" alt="">
                <div class="post-body">
                    <div class="post-options">
                        <a class="likes" href="">2 likes</a>
                    </div>
                    <p>
                        <a class="username" href="">
                                <?php echo $rowfeed['users.username'] ?>
                        </a>
                        <?php echo $rowfeed['posts.body'] ?>
                    </p>
                    <hr />
                    <div class="cf">
                        <a class="like hide-text" href="javascript:;">Like This Post</a>
                        <form action="" class="comment">
                            <input type="text" placeholder="Add a comment">
                        </form>
                    </div>
                </div>
            </article>

   <?php } else { ?>

My tables look like this

users
user_id | username | avatar

posts
post_id | user_id | body | image | has_image

follows
leader_id | follower_id

From your original query, I see that you are only interested in the posts' data (not that of the user who posted it). I assume that $user_id holds the id of the current user, ie a value that is also used as follower_id. If this is the case, the query gets easier:

SELECT p.user_id, p.body, p.image, p.has_image
FROM followers f INNER JOIN posts p ON f.leader_id = p.user_id and f.follower_id = $user_id

If you are interested also in the poster's user data, change the query as follows:

SELECT p.user_id, p.body, p.image, p.has_image, u.username, u.avatar
FROM follows f
  INNER JOIN posts p ON f.leader_id = p.user_id and f.follower_id = $user_id
  INNER JOIN users u ON p.user_id = u.user_id

I have no access to a computer right now but it seems that your SQL-Query has some errors. Check out my corrected version:

SELECT posts.user_id, posts.body, posts.image, posts.has_image FROM posts
INNER JOIN follows ON posts.user_id = follows.follower_id
WHERE follows.leader_id=$user_id;

Try this query:

SELECT
    posts.user_id, posts.body, posts.image, posts.has_image
    authors.avatar, authors.username
FROM
    posts
LEFT JOIN
    users as authors
ON
    authors.user_id=posts.user_id
LEFT JOIN
    follows
ON
    follows.leader_id=authors.user_id
WHERE
    follows.follower_id={$user_id}

Using LEFT JOINs ONLY....

let's assume that the current user.user_id is 1 and followers are user.user_id 2,3, and 4

I would think that we need to meet the following criteria right?

posts.user_id = follows.follower_id follows.leader_id = $user_id

So the query would need to be something like this right?

SELECT * FROM posts INNER JOIN follows ON posts.user_id = follows.follower_id INNER JOIN users ON follows.leader_id = $user_id

Here are mocked examples of the tables

=== users === user_id | username | avatar 1 current 2 fake1 3 fake2 4 fake3 === users === user_id | username | avatar 1 current 2 fake1 3 fake2 4 fake3 === posts === post_id | user_id | body | image | has_image 1 2 2 2 3 3 4 3 5 4 6 4 === posts === post_id | user_id | body | image | has_image 1 2 2 2 3 3 4 3 5 4 6 4 === posts === post_id | user_id | body | image | has_image 1 2 2 2 3 3 4 3 5 4 6 4 === follows === leader_id | follower_id 1 2 1 3 1 4 === follows === leader_id | follower_id 1 2 1 3 1 4 Does that make sense? Because the current user ($user_id) is the leader of the followers, so the follower_id's would need to be the users of the posts if you want the posts of the followers not the post of the leader...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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