MYSQL帮助获取最新帖子或回复

时间:2013-02-27 08:32:41

标签: mysql

我在forum_reply和forum_post下面有以下表格。 我怎么能得到最新的帖子或回复,这是最新的匹配forum_category_id的说法1。 我需要检索日期,作者和论坛_(发布或回复)_id,forum_category_id

forum_post_id   int(11) No  Primary NULL    auto_increment
    forum_category_id   int(11) No  None    NULL    
    forum_post_title    varchar(255)    No  None    NULL    
    forum_post_description  mediumtext  No  None    NULL    
    customer_id int(11) No  None    0   
    forum_post_author   varchar(64) No  None    NULL    
    forum_post_email    varchar(128)    No  None    NULL    
    is_lock tinyint(1)  No  None    0   
    is_stick    tinyint(1)  No  None    0   
    is_notify   tinyint(1)  No  None    0   
    status  tinyint(1)  No  None    0   
    view    int(8)  No  None    0   
    post_added  datetime    No  None    0000-00-00 00:00:00

post_rate   int(8)  No  None    0   
    ip  varchar(32) No  None    NULL    
    post_icon   varchar(128)    No  None    default.png

    forum_reply_id  int(11) No  Primary NULL    auto_increment
    forum_post_id   int(11) No  None    NULL    
    forum_category_id   int(11) No  None    NULL    
    forum_reply_description mediumtext  No  None    NULL    
    customer_id int(11) No  None    0   
    forum_reply_author  varchar(255)    No  None    NULL    
    forum_reply_email   varchar(128)    No  None    NULL    
    is_notify   tinyint(1)  No  None    0   
    status  tinyint(1)  No  None    0   
    reply_added datetime    No  None    0000-00-00 00:00:00 
    reply_rate  int(8)  No  None    0   
    ip  varchar(32) No  None    NULL    
    reply_icon  varchar(128)    No  None    default.png

2 个答案:

答案 0 :(得分:0)

SELECT DISTINCT post_added, forum_post_author, forum_post_id, forum_category_id 
FROM forum_post WHERE forum_category_id = 1 
UNION 
SELECT DISTINCT reply_added, forum_reply_author, forum_reply_id, forum_category_id 
FROM forum_reply WHERE forum_category_id = 1 
ORDER BY post_added DESC LIMIT 1

如果你正在尝试做我认为你正在做的事,我应该这样做(但我也不是那个专业人士)。

此处有关于此的更多信息: Display two different columns from two different tables with ORDER BY

答案 1 :(得分:0)

使用UNIONSELECT语句中的两个表组合起来可以帮助您:

SELECT
    forum_post_author,
    post_added,
    forum_post_id,
    forum_category_id,
    'post' as type
FROM
    forum_post
WHERE
    forum_category_id = 1

UNION
SELECT
    forum_reply_author,
    reply_added,
    forum_reply_id,
    forum_category_id,
    'reply' as type
FROM
    forum_reply
WHERE
    forum_category_id = 1

ORDER BY
    post_added DESC
LIMIT 1

SQL Fiddle