Php 如何修改此查询,以便根据每篇文章的评论数对结果进行排序?

Php 如何修改此查询,以便根据每篇文章的评论数对结果进行排序?,php,mysql,Php,Mysql,这件事我已经坚持了大约一个星期了。具有从数据库获取帖子的SQL查询。我需要添加一个左连接,这将允许我按每个帖子的评论数排序帖子。从评论最多的帖子到评论最少的帖子。这是我的疑问: SELECT DISTINCT wallposts.p_id, wallposts.is_profile_notes, wallposts.times_viewed, wallposts.columnTimesShared, wallposts.marked,wallposts.

这件事我已经坚持了大约一个星期了。具有从数据库获取帖子的SQL查询。我需要添加一个左连接,这将允许我按每个帖子的评论数排序帖子。从评论最多的帖子到评论最少的帖子。这是我的疑问:

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
选择DISTINCT
wallposts.p_id,
wallposts.is_profile_notes,
wallposts.times_查看,
wallposts.ColumnTimeShared,
墙柱。已标记,墙柱。安全标识,
wallposts.reshared,wallposts.group\u id,
wallposts.totaluploads,
wallposts.WallUploadID,
wallposts.type,
wallposts.value,
wallposts.media,
wallposts.youtube,
wallposts.post_类型,
wallposts.隐私,
wallposts.tagedpersons,
墙贴,贴上朋友标签,
wallposts.emotion_head,
wallposts.selected_情绪,
wallposts.title,
wallposts.url,
wallposts.description,
wallposts.cur_图像,
wallposts.uip,
wallposts.likes,
wallposts.userid,
wallposts.posted_by,
wallposts.post作为postdata,
wallusers。*,
UNIX_TIMESTAMP()-wallposts.date_创建为timespunt,
PosterTable.mem_pic作为posterPic,
PosterTable.gender作为posterGender,
PosterTable.oauth\u uid作为poster\u oauth\u uid,
PosterTable.username作为posterUsername,
PosterTable.mem_fname作为posterFname,
作为后期工作,
PosterTable.mem_lname作为posterLname,
walllikes_track.id作为PostLikeFound,
wallposts.date_创建
从…起
wallusers,
wallusers作为PosterTable,
墙柱
左连接
瓦卢赛道
在…上
wallposts.p_id=walllikes_track.post_id
及
walllikes_track.member_id=“.$user_id.”
哪里
wallusers.active=1
及
PosterTable.active=1
及
wallposts.group_id,单位为(“.$groups.”)
及
wallposts.group_id!=0
及
PosterTable.mem_id=wallposts.posted_by
及
wallposts.marked<“$this->flagNumber。”
及
wallusers.mem_id=wallposts.posted_by
“wallposts”是包含my posts的表的名称,具有p_id。“wallcomments”是包含注释的表的名称,其中包含一个名为post_id的列,用于将其链接到posts。请帮助我按评论的数量排列帖子。我尝试过这一点,但每次我的屏幕变为空白,我的程序都会崩溃:

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
选择DISTINCT
wallposts.p_id,
wallposts.is_profile_notes,
wallposts.times_查看,
wallposts.ColumnTimeShared,
墙柱。已标记,墙柱。安全标识,
wallposts.reshared,wallposts.group\u id,
wallposts.totaluploads,
wallposts.WallUploadID,
wallposts.type,
wallposts.value,
wallposts.media,
wallposts.youtube,
wallposts.post_类型,
wallposts.隐私,
wallposts.tagedpersons,
墙贴,贴上朋友标签,
wallposts.emotion_head,
wallposts.selected_情绪,
wallposts.title,
wallposts.url,
wallposts.description,
wallposts.cur_图像,
wallposts.uip,
wallposts.likes,
wallposts.userid,
wallposts.posted_by,
wallposts.post作为postdata,
wallusers。*,
UNIX_TIMESTAMP()-wallposts.date_创建为timespunt,
PosterTable.mem_pic作为posterPic,
PosterTable.gender作为posterGender,
PosterTable.oauth\u uid作为poster\u oauth\u uid,
PosterTable.username作为posterUsername,
PosterTable.mem_fname作为posterFname,
作为后期工作,
PosterTable.mem_lname作为posterLname,
walllikes_track.id作为PostLikeFound,
wallposts.date_创建,
p、 p_id,
c、 后计数
从…起
wallusers,
wallusers作为PosterTable,
墙柱,
墙柱为p
内连接(
挑选
邮政编码,
将(*)计数为后计数
从…起
华尔街评论
分组
邮政编码
)as c
在…上
p、 p_id=c.post_id
左连接
瓦卢赛道
在…上
wallposts.p_id=walllikes_track.post_id
及
walllikes_track.member_id=“.$user_id.”
哪里
wallusers.active=1
及
PosterTable.active=1
及
wallposts.group_id,单位为(“.$groups.”)
及
wallposts.group_id!=0
及
PosterTable.mem_id=wallposts.posted_by
及
wallposts.marked<“$this->flagNumber。”
及
wallusers.mem_id=wallposts.posted_by

我做错了什么?

试着朝相反的方向做

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 ...
groupby
orderby
使用列索引/数字而不是列名应该可以很好地工作,至少在MySQL和Oracle上可以,也许其他的也可以

我想说的是,在您的查询中,您试图“从多个表中获取所有内容”,然后按“另一个表中的某些数据”排序,可能会有帮助的是,首先根据您的需要,将注意力集中在获取按注释数排序的post_id列表上,然后“充实”每个记录(通过加入post_id)使用其他表中的数据

您还可以在comments表上创建一个视图,该视图只显示post\u id,不显示任何注释

稍后编辑:在仔细查看您的查询之后,我觉得它有一些不合理的地方。。。例如,为什么要使用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
选择
c、 *,-“计数收集”数据(也确保了不同的post_id,因为它有group by
p、 *,--发布字段
u、 *,--用户数据
l、 *--喜欢数据
从…起
(选择post_id,按post_id从wallcomments组中将计数(1)作为注释计数)作为c
在p.p_id=c.post_id上以p的形式左连接墙柱
在u.mem\u id=p.posted\u by上以u的身份左键连接wallusers
在l.post\U id=p.p\U id上以l的形式左连接walllikes\U轨迹
哪里
l、 member_id=“.$user_id.”和
p、 组id(“.$groups.”)和
p、 已标记<“$this->flagNumber。”
按c.comment\u count DESC订购
你得调整一下