-1

我在 mysql 中需要一些建议,假设我们有 3 个表:

删除日志:

此表显示所有软删除的线程和帖子以及删除时间的日期:

primaryid   type      dateline
       12   thread    1372789192
       13   thread    1372860700
       15   thread    1372798567
       16   thread    1372818402
       17   thread    1372931230
       37   post      1372790038
       43   post      1372798587

线:

此表显示所有线程及其fourmid:

threadid   forumid  
      12         2
      13         2
      14         2
      15         4
      16         2
      17         2 

邮政:

此表显示所有帖子及其 threadid:

    postid  threadid  
    35        12
    36        13
    37        13
    38        13
    39        12
    40        12
    41        14
    42        14
    43        14
    44        15
    45        16
    46        17

我想要的是显示已删除线程和已删除帖子的最后日期以及其他表格信息,例如 forumid:

primaryid   type     forumid    dateline
       17   thread         2    1372931230
       43   post           2    1372931230

好的,我设法获得了这样的帖子和线程的最后删除日期:

SELECT        *
FROM          deletionlog AS a
INNER JOIN     
(
    SELECT    type, Max(dateline) AS dateline
    FROM      deletionlog
    GROUP BY  type
) AS b
ON            a.type = b.type
AND           a.dateline = b.dateline
WHERE b.type in('thread','post')";

但这只有一张表,我还想加入表线程并发布以提取fourmid,所以有什么建议吗?

4

1 回答 1

1

不幸的是,您提供的数据库结构甚至不符合第二范式。因此,我不得不使用 UNION 编写这个丑陋的请求:

SELECT
    deletionlog.primaryid,
    deletionlog.type,
    thread.forumid,
    deletionlog.dateline
FROM
    deletionlog
INNER JOIN
    thread
ON
    deletionlog.primaryid=thread.threadid
WHERE
    deletionlog.type='thread'
GROUP BY
    deletionlog.type
HAVING
    MAX(deletionlog.dateline)

UNION

SELECT
    deletionlog.primaryid,
    deletionlog.type,
    thread.forumid,
    deletionlog.dateline
FROM
    deletionlog
INNER JOIN
    post
ON
    deletionlog.primaryid=post.postid
INNER JOIN
    thread
ON
    post.threadid=thread.threadid
WHERE
    deletionlog.type='post'
GROUP BY
    deletionlog.type
HAVING
    MAX(deletionlog.dateline)
于 2013-07-04T21:32:21.220 回答