0

我的网站上有一个简单的论坛,几乎是完美的。如此接近,却又如此遥远……

以下 SQL 用于创建论坛的首页视图,用户可以在其中查看线程和回复者的摘要:

SELECT COUNT(forumtopics.forumtopicline)  AS totalthreads
  , forumtopicline
  , forumtopics.whenadded             AS threaddate
  , forumtopics.whoadded              AS author
  , forumtopics.title
  , COUNT(forumreply.forumtopic)      AS replies
  , MAX(forumreply.whenadded)         AS replydate
  , forumreply.whoadded               AS replier
 FROM forumtopics
   LEFT JOIN forumreply ON  forumtopicline = forumreply.forumtopic
WHERE  forumtopics.topic LIKE '%%'
   OR forumtopics.title LIKE '%%'
   OR forumreply.reply LIKE '%%'
GROUP BY forumtopicline
ORDER BY
   CASE 
        WHEN COUNT(forumreply.forumtopic) > 0 THEN MAX(forumreply.whenadded)
        WHEN COUNT(forumreply.forumtopic) = 0
             OR forumreply.forumtopic IS NULL THEN MAX(forumtopics.whenadded)
   END 
   DESC
LIMIT 0 , 30

几乎完美地完成了这项工作。这为我提供了线程开始的日期、线程作者和最后回复的日期,并且它正确地将它们与顶部有最新回复的帖子进行了排名。

问题在于

MAX( forumreply.whenadded ) AS replydate, forumreply.whoadded AS replier

这会将最后一个帖子的日期返回到线程,但无论日期如何,都会返回第一响应者的姓名。我确信必须有一种方法来完成这个相当优雅的 SQL,以避免应用程序级别的操作。感激地收到任何帮助。很明显,我需要 forumreply.whoadded 与 MAX(forumreply.whenadded) 来自同一行

数据库=Mysql。架构:

CREATE TABLE forumtopics (
    forumtopicline     SERIAL
   ,title              VARCHAR(256)
   ,topic              TEXT
   ,whenadded          DATETIME
   ,whoadded           VARBINARY(128)
   ,whenaltered        DATETIME
   ,whoaltered         VARBINARY(128)
   ,inuse              BOOL
);

CREATE TABLE forumreply (
    forumreplyline     SERIAL
   ,forumtopic         BIGINT
   ,reply              TEXT
   ,whenadded          DATETIME
   ,whoadded           VARBINARY(128)
   ,whenaltered        DATETIME
   ,whoaltered         VARBINARY(128)
   ,inuse              BOOL
);
4

1 回答 1

1

尝试这个:

SELECT
    totalthreads,
    forumtopicline,
    threaddate,
    author,
    title,
    replies,
    replydate,
    (
        SELECT
            fr.whoadded
        FROM
            forumreply fr
        WHERE
            fr.forumtopic = forumtopicline
            AND fr.reply LIKE '%%'
            AND fr.whenadded = replydate
    ) as replier
FROM (
    SELECT COUNT(forumtopics.forumtopicline)  AS totalthreads
      , forumtopicline
      , forumtopics.whenadded             AS threaddate
      , forumtopics.whoadded              AS author
      , forumtopics.title
      , COUNT(forumreply.forumtopic)      AS replies
      , MAX(forumreply.whenadded)         AS replydate
      , forumreply.whoadded               AS replier
     FROM forumtopics
       LEFT JOIN forumreply ON  forumtopicline = forumreply.forumtopic
    WHERE  forumtopics.topic LIKE '%%'
       OR forumtopics.title LIKE '%%'
       OR forumreply.reply LIKE '%%'
    GROUP BY forumtopicline
    ORDER BY
       CASE 
            WHEN COUNT(forumreply.forumtopic) > 0 THEN MAX(forumreply.whenadded)
            WHEN COUNT(forumreply.forumtopic) = 0
                 OR forumreply.forumtopic IS NULL THEN MAX(forumtopics.whenadded)
       END 
       DESC
) as tmp
于 2013-05-31T10:14:14.570 回答