通过最新活动对特定论坛中的线程进行排序

时间:2016-06-17 15:08:01

标签: mysql

我有3张桌子:

论坛,主题,评论

我想在特定论坛中对所有线程进行排序,考虑线程发布的时间以及线程中发布的最后一条评论。

线程中的每一行都有一列" fid"它显示了帖子在哪个论坛发布。

评论中的每一行都有一列" tid"它显示评论发布在哪个主题上。

完成任务的正确查询是什么?

编辑:

结构:

论坛 - id,name

主题 - id,fid(论坛ID),标题,时间

评论 - id,tid(线程ID),内容,时间

我希望线程按此顺序显示,例如:

SELECT * FROM threads INNER JOIN comments ON  threads.id = comments.tid WHERE threads.fid = 3
GROUP BY threads.id ORDER BY MAX(comments.time)  DESC

到目前为止我已尝试过这个:

public static BufferedImage toBufferedImage(byte[] input) throws IOException {
    InputStream in = new ByteArrayInputStream(input);
    BufferedImage bimg = ImageIO.read(in);
    return bimg;
}

public static BufferedImage[] extract(final String fileName) throws IOException {
    PdfReader reader = new PdfReader(fileName);
    int pageNum = reader.getNumberOfPages();
    BufferedImage[] imgArray = new BufferedImage[pageNum];

    for (int page = 0; page < pageNum; page++) {
        //TODO: You may need to decode the bytearray first?
        imgArray[page] = toBufferedImage(reader.getPageContent(pageNum)); 
    }

    reader.close();
    return imgArray;
}

public static void convert() throws IOException {
    String fileName = getProps("file_in");
        BufferedImage[] bim = extract(fileName);
        // close streams; Closed implicitily by try-with-resources

}

1 个答案:

答案 0 :(得分:0)

尝试

SELECT `t`.`id`, `t`.`title`, `t`.`time`, `c`.`comment_time`  
FROM `threads` t
LEFT JOIN (SELECT `tid`, MAX(`time`) AS comment_time FROM `comments` GROUP BY  `tid`) c
ON `t`.`id` = `c`.`tid`
ORDER BY `t`.`time`, `c`.`comment_time` desc
相关问题