MySQL在具有多个联接的组内排序

MySQL在具有多个联接的组内排序,mysql,join,group-by,Mysql,Join,Group By,我正在尝试使用以下表格: 用户(id、名称) 主题(id、名称、slug) 帖子(id、标题、内容、主题id、作者、日期时间、slug) 答复(id、authorid、threadid、内容、日期时间) 我想创建一个由最新回复(或发布日期,如果没有回复)排序的帖子列表。每个列表应包含:帖子标题、回复数量、帖子日期、最新回复日期、作者姓名、最后回复者姓名等 除了最新回复的日期和作者之外,我什么都可以得到。日期可以通过MAX(repries.datetime)完成,但我不确定如何获取最新作者 我上次

我正在尝试使用以下表格: 用户(id、名称) 主题(id、名称、slug) 帖子(id、标题、内容、主题id、作者、日期时间、slug) 答复(id、authorid、threadid、内容、日期时间)

我想创建一个由最新回复(或发布日期,如果没有回复)排序的帖子列表。每个列表应包含:帖子标题、回复数量、帖子日期、最新回复日期、作者姓名、最后回复者姓名等

除了最新回复的日期和作者之外,我什么都可以得到。日期可以通过MAX(repries.datetime)完成,但我不确定如何获取最新作者

我上次尝试使用排除联接:

select posts.id, posts.title, posts.authorid, posts.topicid, posts.content, posts.datetime, count(replies.id) as replies, r2.datetime, replies.datetime, GREATEST(posts.datetime, coalesce(max(replies.datetime), posts.datetime)) as latesttime, users.name as author, commenters.name as commenter, r2.id 
from posts 
left join replies on replies.threadid = posts.id 
left join users on users.id = posts.authorid 
left join users commenters on commenters.id = replies.authorid
left join replies as r2 on replies.id = r2.id and replies.datetime < r2.datetime 
where r2.id is null 
group by posts.id 
order by latesttime DESC, replies.datetime DESC 
limit 20;
选择posts.id、posts.title、posts.authorid、posts.topicid、posts.content、posts.datetime、count(replets.id)作为回复、r2.datetime、replets.datetime、maxist(posts.datetime、coalesce(max(replets.datetime)、posts.datetime))作为最晚时间、users.name作为作者、commenters.name作为注释者、r2.id
发帖
left join replies on replies.threadid=posts.id
在users.id=posts.authorid上左加入用户
left join users commenters on commenters.id=replies.authorid
在reples.id=r2.id和reples.datetime

不幸的是,这仍然无法检索最新的评论作者。有什么想法吗?

您必须首先从给定线程的最大ID的最内部查询开始,并将其回复ID加入原始回复以获得一个实例。。。从中,您可以获得回复作者的信息。然后,根据需要获取原始发布信息

SELECT 
      p.id,
      p.title,
      p.content,
      p.topicid,
      p.authorid,
      p.datetime as postdate,
      p.slug,
      postUser.Name as PostAuthor,
      coalesce( MaxReplyUser.NumReplies, 0 ) NumReplies,
      coalesce( MaxReplyUser.name, '' ) ReplyUser,
      coalesce( MaxReplyUser.AuthorID, 0 ) ReplyAuthor,
      coalesce( MaxReplyUser.ThreadID, 0 ) ReplyThread,
      coalesce( MaxReplyUser.DateTime, '' ) ReplyDateTime,
      coalesce( MaxReplyUser.Content, '' ) ReplyContent
   from 
      Posts p
         left join 
            ( SELECT 
                     u1.name,
                     r1.authorid,
                     r1.threadid,
                     r1.datetime,
                     r1.content,
                     MaxReplies.NumReplies
                  from 
                    ( SELECT 
                            threadid,
                            COUNT(*) as NumReplies,
                            MAX( id ) as MaxReplyID
                         from 
                            replies
                         group by 
                            threadID ) MaxReplies
                       INNER JOIN replies r1
                          ON MaxReplies.MaxReplyID = r1.id
                          INNER JOIN Users u1
                             ON r1.AuthorID = u1.ID ) MaxReplyUser
           ON p.id = MaxReplyUser.threadID
         inner join users postUser
            ON p.authorid = postuser.id
    order by 
       if( MaxReplyUser.threadID IS NULL, p.DateTime, MaxReplyUser.DateTime ) DESC
你能测试一下吗

SELECT posts.id, posts.title, posts.authorid, posts.topicid, posts.content, posts.datetime, 
(SELECT name FROM users WHERE id = posts.autorid) "Author",
(SELECT COUNT(*) FROM replies WHERE threadid = posts.id) "Replies",
(SELECT MAX(datetime) FROM replies r WHERE threadid = posts.id) "Lastreplytime",
(SELECT (SELECT name FROM users WHERE id = r.authorid LIMIT 1) FROM replies r WHERE threadid = posts.id ORDER BY datetime DESC LIMIT 1)
FROM posts 
ORDER BY Lastreplytime DESC
LIMIT 20;

这工作得很好,尽管看起来用CodeIgniter的活动记录系统实现可能有点像噩梦!有没有一种方法可以在没有选择的情况下做到这一点?(还有,新手问题:选择是否比加入更有效?@RobL,Joseadrian的另一个解决方案似乎也能起作用,即使使用我为测试我的版本而创建的虚假样本数据。。。它也有SELECTS作为列嵌入,但无论哪种方式都有SELECTS。只是我自己测试了它,它确实工作了!再一次,对选择有点警惕。谢谢你的帮助!正如上面DRapp所指出的,这个版本也可以工作。我对这里select语句的数量有点担心,但我不知道这是否比使用联接更有效。我在一个项目的查询中尝试了20多个联接。此查询具有按订单分组等。。。而且它比进行子查询要慢。获得5000多个结果只需一分钟,而大量子查询只需2到3秒:xWell,我希望避免出现多达20个连接或子查询!不过谢谢你的提示-请记住!