如何加入帖子表和评论表,以便我可以按评论数量最多的帖子对结果进行排序?

时间:2017-04-25 23:17:57

标签: php mysql

嘿,伙计们,我似乎无法解决这个问题。我需要天才思想的帮助。我有一个返回所有帖子的函数。但我需要能够通过评论数量最多的帖子对它们进行排序。这是我的PHP函数,其中包含SQL。

function GetNewsfeedPosts( $matches, $user_id, $show_more_post, $followers = 0)
{
    global $DBConnection;
    $groups = $this->SubscribedGroupsIDs($user_id);/* get all of the groups that I am a part of */
    $union  = '';/* declare the variable */
    $ratingQuery = 'p_id';/* declare the variable and set the default to all ratings */
    /*----------------filtering the home posts----------------*/
    if($_SESSION['ratingFilter'] == 0) {/* if the selected rating filter is all ratings then */
        $ratingQuery = 'p_id';
    } else if($_SESSION['ratingFilter'] == 1) {/* if the selected rating filter is most loves then */
        $ratingQuery = 'likes';
    } else if($_SESSION['ratingFilter'] == 2) {/* if the selected rating filter is most comments then */
        $ratingQuery = 'likes';
    } else if($_SESSION['ratingFilter'] == 3) {/* if the selected rating filter is most shares then */
        $ratingQuery = 'likes';
    } else if($_SESSION['ratingFilter'] == 4) {/* if the selected rating filter is most views then */
        $ratingQuery = 'likes';
    }

    if($groups != 0)
    {
        $union = "UNION ALL
                    ( 
                        SELECT DISTINCT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,
                        wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
                        wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
                        wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
                        wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
                        wallposts.url,wallposts.description,wallposts.cur_image,
                        wallposts.uip,wallposts.likes,wallposts.userid,
                        wallposts.posted_by,wallposts.post as postdata,wallusers.*, 
                        UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,

                        PosterTable.mem_pic as posterPic, PosterTable.gender as posterGender,PosterTable.oauth_uid as poster_oauth_uid, PosterTable.username as posterUsername, 
                        PosterTable.mem_fname as posterFname,PosterTable.work as posterWork, 
                        PosterTable.mem_lname as posterLname,walllikes_track.id as PostLikeFound,wallposts.date_created 

                        FROM 

                        wallusers,wallusers as PosterTable, wallposts 

                        LEFT JOIN walllikes_track ON wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id." 

                        WHERE 
                        wallusers.active = 1 
                        AND PosterTable.active = 1 AND 
                        wallposts.group_id IN (".$groups.")  AND wallposts.group_id != 0
                        AND PosterTable.mem_id = wallposts.posted_by AND wallposts.marked < ".$this->flagNumber."
                        AND wallusers.mem_id = wallposts.posted_by ) "; 
    }

    if($followers != 0)
    {
        $union .= "UNION ALL
                    ( 
                        SELECT DISTINCT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,
                        wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
                        wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
                        wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
                        wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,
                        wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
                        wallposts.url,wallposts.description,wallposts.cur_image,
                        wallposts.uip,wallposts.likes,wallposts.userid,
                        wallposts.posted_by,wallposts.post as postdata,wallusers.*, 
                        UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,

                        PosterTable.mem_pic as posterPic, 
                        PosterTable.gender as posterGender,
                        PosterTable.oauth_uid as poster_oauth_uid, 
                        PosterTable.username as posterUsername, 
                        PosterTable.mem_fname as posterFname,
                        PosterTable.work as posterWork, 
                        PosterTable.mem_lname as posterLname,
                        walllikes_track.id as PostLikeFound,
                        wallposts.date_created 

                        FROM 

                        wallusers,wallusers as PosterTable, 
                        wallposts LEFT JOIN walllikes_track ON wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id." 

                        WHERE 
                        wallusers.active = 1 
                        AND PosterTable.active = 1 AND
                        wallusers.mem_id IN (".$followers.") 

                        AND wallposts.privacy = 1

                        AND PosterTable.mem_id = wallposts.posted_by  

                        AND wallposts.marked < ".$this->flagNumber."

                        AND wallusers.mem_id = wallposts.userid ) ";    
    }

    // updated for groups
    $qry = "SELECT * FROM (
            (
                SELECT DISTINCT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,
                wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
                wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
                wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
                wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,
                wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
                wallposts.url,wallposts.description,wallposts.cur_image,
                wallposts.uip,wallposts.likes,wallposts.userid,
                wallposts.posted_by,wallposts.post as postdata,wallusers.*, 
                UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,

                PosterTable.mem_pic as posterPic, 
                PosterTable.gender as posterGender,
                PosterTable.oauth_uid as poster_oauth_uid, 
                PosterTable.username as posterUsername, 
                PosterTable.mem_fname as posterFname,
                PosterTable.work as posterWork, 
                PosterTable.mem_lname as posterLname,

                walllikes_track.id as PostLikeFound,
                wallposts.date_created 

                FROM 

                wallusers,wallusers as PosterTable, 
                wallposts LEFT JOIN walllikes_track ON wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id." 

                WHERE 
                wallusers.active = 1 
                AND PosterTable.active = 1 AND
                (wallusers.mem_id IN (".$matches.") OR wallusers.mem_id = ".$user_id.")

                AND PosterTable.mem_id = wallposts.posted_by  AND wallposts.marked < ".$this->flagNumber."

                AND wallusers.mem_id = wallposts.userid )

                ".$union ."
            ) 
            results
            order by $ratingQuery desc limit ".$show_more_post.", 10"; 

    // GROUP PRIVACY 
    // 2 = public, 
    // 1 = closed, 
    // 0 = secret       

    // POSTS PRIVACY 
    // pub = 1 
    // annoy = 2
    // on newsfeed, show all posts including public, annoymous and even those are not friends. AND wallposts.privacy != 2

    $res = mysqli_query($DBConnection, $qry);
    return $res;
}

带有评论的表名为&#34; wallcomments&#34;它有列post_id,Comment_id等。如何在一个查询中加入这两个表,以便我可以返回评论数最多的帖子。我不知道在哪里放入加入条款或选择原因我不知道如何编写它。请帮忙。

0 个答案:

没有答案