MySQL ORDER BY然后组

时间:2016-03-02 11:36:03

标签: mysql

我有这样的查询:

SELECT
    `Thread`.`ID` AS `ThreadID`,
    `Post`.`CreatorName` AS `LastPostBy`,
    `Post`.`CreatorUserID` AS `LastPostByID`,
    `Post`.`CreationDate` AS `LastPostDate`
FROM
    `Post`
        INNER JOIN
    `Thread` ON `Post`.`ThreadID` = `Thread`.`ID`
ORDER BY
    `Post`.`CreationDate` DESC

产生以下结果:

+---------+------------+--------------+--------------------+
|ThreadID | LastPostBy | LastPostByID | LastPostDate       |
+---------+------------+--------------+--------------------+
|       3 | User2      |           13 | 2016-03-02 09:38:44|
|       2 | User1      |            1 | 2016-03-01 09:27:24|
|       2 | User1      |            1 | 2016-02-14 21:27:06|
+---------+------------+--------------+--------------------+

现在我想让结果按ThreadID分组,所以我添加了这样的GROUP BY子句:

SELECT
    `Thread`.`ID` AS `ThreadID`,
    `Post`.`CreatorName` AS `LastPostBy`,
    `Post`.`CreatorUserID` AS `LastPostByID`,
    `Post`.`CreationDate` AS `LastPostDate`
FROM
    `Post`
        INNER JOIN
    `Thread` ON `Post`.`ThreadID` = `Thread`.`ID`
GROUP BY
    `Thread`.`ID`
ORDER BY
    `Post`.`CreationDate` DESC

我得到以下结果:

+---------+------------+--------------+--------------------+
|ThreadID | LastPostBy | LastPostByID | LastPostDate       |
+---------+------------+--------------+--------------------+
|       3 | User2      |           13 | 2016-03-02 09:38:44|
|       2 | User1      |            1 | 2016-02-14 21:27:06|
+---------+------------+--------------+--------------------+

然而,我的预期结果是该记录与最新的LastPostDate分组如下:

+---------+------------+--------------+--------------------+
|ThreadID | LastPostBy | LastPostByID | LastPostDate       |
+---------+------------+--------------+--------------------+
|       3 | User2      |           13 | 2016-03-02 09:38:44|
|       2 | User1      |            1 | 2016-03-01 09:27:24|
+---------+------------+--------------+--------------------+

如何获得这样的结果?

这个问题叫什么名字?所以将来我可以自己搜索。

非常感谢。

编辑:

YossiVipin Jain提供的解决方案适用于第一个问题。

然而,当数据是这样的时候:

+---------+------------+--------------+--------------------+
|ThreadID | LastPostBy | LastPostByID | LastPostDate       |
+---------+------------+--------------+--------------------+
|       3 | User1      |            1 | 2016-03-02 18:55:19|
|       3 | User2      |           13 | 2016-03-02 09:38:44|
|       2 | User1      |            1 | 2016-03-01 09:27:24|
|       2 | User1      |            1 | 2016-02-14 21:27:06|
+---------+------------+--------------+--------------------+

提供的解决方案会产生以下结果:

+---------+------------+--------------+--------------------+
|ThreadID | LastPostBy | LastPostByID | LastPostDate       |
+---------+------------+--------------+--------------------+
|       3 | User2      |           13 | 2016-03-02 18:55:19|
|       2 | User1      |            1 | 2016-03-01 09:27:24|
+---------+------------+--------------+--------------------+

应该是这样的:

+---------+------------+--------------+--------------------+
|ThreadID | LastPostBy | LastPostByID | LastPostDate       |
+---------+------------+--------------+--------------------+
|       3 | User1      |            1 | 2016-03-02 18:55:19|
|       2 | User1      |            1 | 2016-03-01 09:27:24|
+---------+------------+--------------+--------------------+

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

您缺少MAX()函数:

null

编辑:

SELECT
    `Thread`.`ID` AS `ThreadID`,
    `Post`.`CreatorName` AS `LastPostBy`,
    `Post`.`CreatorUserID` AS `LastPostByID`,
    max(`Post`.`CreationDate`) AS `LastPostDate`
FROM
    `Post`
        INNER JOIN
    `Thread` ON `Post`.`ThreadID` = `Thread`.`ID`
GROUP BY
    `Thread`.`ID`
ORDER BY
    `Post`.`CreationDate` DESC

答案 1 :(得分:1)

您可以使用max

获取用户的MAX()日期
SELECT
    `Thread`.`ID` AS `ThreadID`,
    `Post`.`CreatorName` AS `LastPostBy`,
    `Post`.`CreatorUserID` AS `LastPostByID`,
    MAX(`Post`.`CreationDate`) AS `LastPostDate`
FROM
    `Post`
        INNER JOIN
    `Thread` ON `Post`.`ThreadID` = `Thread`.`ID`
GROUP BY
    `Thread`.`ID`
ORDER BY
    `Post`.`CreationDate` DESC