【发布时间】:2012-03-04 11:39:24
【问题描述】:
在 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 |
注意:
- 如果 idPost = idPostParent 则检测到 PARENT 帖子
- 如果 idPost idPostParent 和回复是 detedted 回复帖子 与 idPostParent 中的帖子相关
我需要 SQL 查询以按一些规则对表中的记录集进行排序和分组:
- 帖子必须按 idPostParent 分组(先是父帖子,然后是其 回复)
- 最近输入或最近输入回复的 PARENT 帖子必须位于顶部。
结果集应该是:
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 帖子 idPost=2 是最新输入的回复帖子,因此它的帖子必须位于表格顶部。
感谢任何可能为解决方案做出贡献的人!
【问题讨论】:
-
一般来说,您应该发布您尝试过的内容。这种类型的请求(“这就是我想要的,为我做”)是不合适的,并且可能会被否决。
-
感谢您的建议!下次我会更小心的。
标签: mysql sql sorting grouping