2017-04-27 26 views
0

我一直堅持這一個星期。有一個從數據庫中提取帖子的SQL查詢。我需要添加一個LEFT JOIN,允許我按每個帖子發表的評論數量來排列帖子。從評論最多的帖子到評論最少的帖子。這是我的查詢:如何修改此查詢,以便按每個帖子發表的評論數對結果進行排序?

SELECT DISTINCT 
    wallposts.p_id, 
    wallposts.is_profile_notes, 
    wallposts.times_viewed, 
    wallposts.columnTimesShared, 
    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 

「wallposts」是我的職位表的名稱和具有的p_id。並且「wallcomments」是在其中有註釋的表的名稱,其中有一個名爲post_id的列將其鏈接到帖子。請幫助我需要按評論數量排列帖子。我試過了,但是我的屏幕一片空白,每次程序崩潰:

SELECT DISTINCT 
    wallposts.p_id, 
    wallposts.is_profile_notes, 
    wallposts.times_viewed, 
    wallposts.columnTimesShared, 
    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, 

    p.p_id, 
    c.postcount 

FROM 
    wallusers, 
    wallusers as PosterTable, 
    wallposts, 
    wallposts as p 


INNER JOIN (
       SELECT 
        post_id, 
        count(*) AS postcount 
       FROM 
        wallcomments 
       GROUP BY 
        post_id 
      ) as c 
on 
    p.p_id = c.post_id 


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 

我在做什麼錯?

回答

0

嘗試做它在相反的方向

select a.post_id, ...all the rest ... 
from 
    (select post_id, count(1) as c from wallcomments group by 1 order by 2 desc) a 
left join wallcomments w on w.p_id = a.post_id ... 

group byorder by與列索引/數字而不是列名稱應至少與MySQL和甲骨文,也許別人也

我」什麼m試圖說的是,在您的查詢中,您試圖「從多個表中獲取所有內容」,然後按「另一個表中的某些數據」進行排序,可能有所幫助的是首先關注獲取按照數量排序的post_ids列表評論,然後按照你的意願,然後用數據來填充每條記錄(加入post_id)其他表格。

你也可以創建在意見表,它表明只有post_ids視圖,no_of_comments

後來編輯:以在查詢更好看後,在我看來,它有一些事情,不要」 t加起來......例如,你爲什麼使用wallusers表兩次?你似乎鏈接到它以同樣的方式(也許我失去了一些東西),反正一些擺弄後,我想出了這個

SELECT 
    c.*, -- the "count collection" data (also ensures distinct post_id, because it has group by 
    p.*, -- posts fields 
    u.*, -- user data 
    l.* -- likes data 
FROM 
    (select post_id, COUNT(1) as comment_count FROM wallcomments GROUP BY post_id) as c 
    LEFT JOIN wallposts as p on p.p_id = c.post_id 
    LEFT JOIN wallusers as u on u.mem_id = p.posted_by 
    LEFT JOIN walllikes_track as l on l.post_id = p.p_id 
WHERE 
    l.member_id = ".$user_id." AND 
    p.group_id IN (".$groups.") AND 
    p.marked < ".$this->flagNumber." 
ORDER BY c.comment_count DESC 

你必須調整它,以獲得所需的列表列和添加任何其他條件。

作爲一個側面說明,看看「準備的語句」,而不是建立和執行原始查詢(也檢查SQL注入)

+0

的答案好感謝。我即將嘗試它。 「a.post_id」是否指代評論的編號?這是我唯一需要添加到select子句的東西嗎? –

+0

對於遲到的回覆,我第一次回覆時在我的時區(很晚)。我在原始回覆中添加了一些額外的細節。回答這個問題...... a.post_id是評論表中的「post_id」列。 (從電話答覆是一個真正的痛苦...) –

+0

不用擔心。我感謝你的努力。是的,我試圖做到相反,同樣的事情不斷髮生。該程序崩潰。我想知道是否因爲我正在使用SELECT DISTINCT .... hmmm –

相關問題