MySql:需要SQL查询来按一些规则对表中的记录集进行排序和分组

时间:2012-02-13 00:50:45

标签: mysql sql sorting grouping

在MySql中我有表:

idPost | idPostParent | postText                              | dateCreated         |
1      | 1            |  This is parent post no1              | 2012-01-01 12:00:00 |
2      | 2            |  This is parent post no2              | 2012-01-02 13:00:00 |
3      | 1            |  This is first reply on idPost:1      | 2012-01-02 13:30:00 | 
4      | 4            |  This is parent post no3              | 2012-01-04 10:00:00 |
5      | 2            |  This is first reply on idPost:2      | 2012-01-04 11:00:00 |
6      | 1            |  This is second reply on idPost:1     | 2012-01-05 15:00:00 |
7      | 2            |  This is second reply on idPost:2     | 2012-01-06 17:00:00 |

注意:

  1. 如果idPost = idPostParent
  2. ,则会检测到PARENT帖子 如果idPost<>
  3. REPLY post is detedted idPostParent和回复是 与idPostParent中的post writen相关。
  4. 需要SQL查询来按照某些规则对表中的记录集进行排序和分组

    1. 帖子必须按idPostParent分组(父帖先是然后是它 回复)
    2. 最新输入的PARENT帖子或最新输入的回复必须位于顶部。
    3. 结果集应为:

      idPost | idPostParent | postText                              | dateCreated         |
      2      | 2            |  This is parent post no2              | 2012-01-02 13:00:00 |
      5      | 2            |  This is first reply on idPost:2      | 2012-01-04 11:00:00 |
      7      | 2            |  This is second reply on idPost:2     | 2012-01-06 17:00:00 |
      1      | 1            |  This is parent post no1              | 2012-01-01 12:00:00 |
      3      | 1            |  This is first reply on idPost:1      | 2012-01-02 13:30:00 |
      6      | 1            |  This is second reply on idPost:1     | 2012-01-05 15:00:00 |
      4      | 4            |  This is parent post no3              | 2012-01-04 10:00:00 |
      

      原因: PARENT post idPost = 2有最新输入的回复帖子,因此其主题必须位于表格顶部。

      感谢任何可能为解决方案做出贡献的人!

3 个答案:

答案 0 :(得分:3)

这适用于您的测试数据

SELECT
posts.*
FROM posts
INNER JOIN 
  (
    SELECT idPostParent, MAX(dateCreated) as dateLastAnswer
    FROM posts
    GROUP by idPostParent
  ) AS pp ON posts.idPostParent=pp.idPostParent
ORDER BY pp.dateLastAnswer DESC, idPost ASC

答案 1 :(得分:2)

这是一种方式:

SELECT p.*
FROM posts p
LEFT JOIN
 (SELECT idPostParent, MAX(dateCreated) AS time_of_newest
  FROM posts
  GROUP BY idPostParent) p2
 ON p.idPostParent = p2.idPostParent
ORDER BY time_of_newest DESC, idPost=p.idPostParent DESC, dateCreated DESC

说明:

子查询

SELECT idPostParent, MAX(dateCreated) AS time_of_newest
  FROM posts
  GROUP BY idPostParent

选择每个“线程”(idPostParent)创建的最大日期。

这是主LEFT JOIN表的posts d,因此每个线程都有一个time_of_newest用于该线程。

我们按time_of_newest DESCENDING排序(这会将线程2放在线程4前面的线程1前面。)

然后,我们按idPost=idPostParent1 DESC排序,以便在回复之前获得父帖(父帖在这里有1个,回复所有都有0,因为我们排序降序父帖首先出现。)

最后,我们按dateCreated DESC排序,以便按顺序获得回复。

答案 2 :(得分:0)

这样的东西?

SELECT op.*, (SELECT MAX(ip.dateCreated) FROM posts ip WHERE ip.idPostParent = op.idPostParent) newestParentReply 
FROM posts op ORDER BY newestParentReply DESC, op.dateCreated ASC

尚未测试过。使用必须为每个帖子执行的子查询,它的表现也非常糟糕。