我一直堅持這一個星期。有一個從數據庫中提取帖子的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
我在做什麼錯?
的答案好感謝。我即將嘗試它。 「a.post_id」是否指代評論的編號?這是我唯一需要添加到select子句的東西嗎? –
對於遲到的回覆,我第一次回覆時在我的時區(很晚)。我在原始回覆中添加了一些額外的細節。回答這個問題...... a.post_id是評論表中的「post_id」列。 (從電話答覆是一個真正的痛苦...) –
不用擔心。我感謝你的努力。是的,我試圖做到相反,同樣的事情不斷髮生。該程序崩潰。我想知道是否因爲我正在使用SELECT DISTINCT .... hmmm –