在 Many 2 Many 中排序的递归方式
Recursive way to order by in Many 2 Many
我有 tables/records 这样的:
Table: COMMENTS
---------------------------------------------
COMMENT_ID | CONTENT | CREATE_DATE
---------------------------------------------
1 | Content 1 | 2016-09-01
2 | Content 2 | 2016-09-02
3 | Content 3 | 2016-09-03
4 | Reply 2-1 | 2016-09-04
5 | Reply 1-1 | 2016-09-05
6 | Reply 1-1-1 | 2016-09-06
Table: REPLY_COMMENTS
---------------------------------
COMMENT_ID | REPLY_TO_COMMENT_ID
---------------------------------
4 | 2
5 | 1
6 | 5
我想显示这样的记录:
---------------------------------------------
COMMENT_ID | CONTENT | CREATE_DATE
---------------------------------------------
1 | Content 1 | 2016-09-01
5 | Reply 1-1 | 2016-09-05
6 | Reply 1-1-1 | 2016-09-06
2 | Content 2 | 2016-09-02
4 | Reply 2-1 | 2016-09-04
3 | Content 3 | 2016-09-03
所以 'reply' 内容应该在父内容下面 - 但回复内容也应该按 CREATE_DATE 排序。
基本上,我想把:内容放在一起,然后按 CREATE_DATE 的顺序回复。
我这样写查询:
SELECT comment.*
FROM COMMENTS comment
LEFT JOIN REPLY_COMMENTS reply_comment ON reply_comment.COMMENT_ID = comment.COMMENT_ID
ORDER BY (SOMETHING SHOULD BE HERE), comment.CREATE_DATE ASC
我无法用我目前的知识编写 order by 子句 - 请帮助我(我正在使用 MySQL)。
如果你总是在 char 和数值之间有 space 那么它工作正常(对于当前数据)否则你可以尝试以巧妙的方式使用字符串函数:
COMMENT_ID CONTENT CREATE_DATE
---------- ------------- -----------
1 Content 1 2016-09-01
2 Content 2 2016-09-02
3 Content 3 2016-09-03
4 Reply 2-1 2016-09-04
5 Reply 1-1 2016-09-05
6 Reply 1-1-1 2016-09-06
7 Reply 1-10-12 2016-09-06
SELECT comment.*
FROM COMMENTS COMMENT
LEFT JOIN REPLY_COMMENTS reply_comment ON reply_comment.COMMENT_ID = comment.COMMENT_ID
ORDER BY REPLACE(content, LEFT(content, LOCATE(' ', content) - 1), '')
--OUTPUT--
COMMENT_ID CONTENT CREATE_DATE
---------- ------------- -----------
1 Content 1 2016-09-01
5 Reply 1-1 2016-09-05
6 Reply 1-1-1 2016-09-06
7 Reply 1-10-12 2016-09-06
2 Content 2 2016-09-02
4 Reply 2-1 2016-09-04
3 Content 3 2016-09-03
我认为这是不可能的 - 所以我最终添加了新列:SEQNO
到 COMMENTS
table。插入新评论时将更新该列。
我有 tables/records 这样的:
Table: COMMENTS
---------------------------------------------
COMMENT_ID | CONTENT | CREATE_DATE
---------------------------------------------
1 | Content 1 | 2016-09-01
2 | Content 2 | 2016-09-02
3 | Content 3 | 2016-09-03
4 | Reply 2-1 | 2016-09-04
5 | Reply 1-1 | 2016-09-05
6 | Reply 1-1-1 | 2016-09-06
Table: REPLY_COMMENTS
---------------------------------
COMMENT_ID | REPLY_TO_COMMENT_ID
---------------------------------
4 | 2
5 | 1
6 | 5
我想显示这样的记录:
---------------------------------------------
COMMENT_ID | CONTENT | CREATE_DATE
---------------------------------------------
1 | Content 1 | 2016-09-01
5 | Reply 1-1 | 2016-09-05
6 | Reply 1-1-1 | 2016-09-06
2 | Content 2 | 2016-09-02
4 | Reply 2-1 | 2016-09-04
3 | Content 3 | 2016-09-03
所以 'reply' 内容应该在父内容下面 - 但回复内容也应该按 CREATE_DATE 排序。
基本上,我想把:内容放在一起,然后按 CREATE_DATE 的顺序回复。
我这样写查询:
SELECT comment.*
FROM COMMENTS comment
LEFT JOIN REPLY_COMMENTS reply_comment ON reply_comment.COMMENT_ID = comment.COMMENT_ID
ORDER BY (SOMETHING SHOULD BE HERE), comment.CREATE_DATE ASC
我无法用我目前的知识编写 order by 子句 - 请帮助我(我正在使用 MySQL)。
如果你总是在 char 和数值之间有 space 那么它工作正常(对于当前数据)否则你可以尝试以巧妙的方式使用字符串函数:
COMMENT_ID CONTENT CREATE_DATE
---------- ------------- -----------
1 Content 1 2016-09-01
2 Content 2 2016-09-02
3 Content 3 2016-09-03
4 Reply 2-1 2016-09-04
5 Reply 1-1 2016-09-05
6 Reply 1-1-1 2016-09-06
7 Reply 1-10-12 2016-09-06
SELECT comment.*
FROM COMMENTS COMMENT
LEFT JOIN REPLY_COMMENTS reply_comment ON reply_comment.COMMENT_ID = comment.COMMENT_ID
ORDER BY REPLACE(content, LEFT(content, LOCATE(' ', content) - 1), '')
--OUTPUT--
COMMENT_ID CONTENT CREATE_DATE
---------- ------------- -----------
1 Content 1 2016-09-01
5 Reply 1-1 2016-09-05
6 Reply 1-1-1 2016-09-06
7 Reply 1-10-12 2016-09-06
2 Content 2 2016-09-02
4 Reply 2-1 2016-09-04
3 Content 3 2016-09-03
我认为这是不可能的 - 所以我最终添加了新列:SEQNO
到 COMMENTS
table。插入新评论时将更新该列。