MySQL 中的 Many 2 Many 按子句排序
Order by clause for Many 2 Many in MySQL
我有 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 to Content 2 | 2016-09-04
5 | Reply to Content 1 | 2016-09-05
6 | Reply to Content 2 | 2016-09-03
Table: REPLY_COMMENTS
---------------------------------
COMMENT_ID | REPLY_TO_COMMENT_ID
---------------------------------
4 | 2
5 | 1
6 | 2
我想显示这样的记录:
---------------------------------------------
COMMENT_ID | CONTENT | CREATE_DATE
---------------------------------------------
1 | Content 1 | 2016-09-01
5 | Reply to Content 1 | 2016-09-05
2 | Content 2 | 2016-09-02
6 | Reply to Content 2 | 2016-09-03
4 | Reply to Content 2 | 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)。
我 只想使用 COMMENTS.CREATE_DATE
字段 - 不想使用 COMMENT_ID
字段,因为它是主键(甚至可能吗? ).
SELECT t1.COMMENT_ID,
t1.CONTENT,
t1.CREATE_DATE
FROM COMMENTS t1
LEFT JOIN REPLY_COMMENTS t2
ON t1.COMMENT_ID = t2.COMMENT_ID
ORDER BY COALESCE(t2.REPLY_TO_COMMENT_ID, t1.COMMENT_ID),
t1.CREATE_DATE
解释:
ORDER BY
子句使用两个术语进行排序。第一个 COALESCE
项将 return parent 消息的 COMMENT_ID
(对于 parent 和单个后代 children)。这样做的原因是对于 children 它将使用连接的 ID,对于 parents,找到 NULL 它也将默认为 parent ID。第二个排序项使用创建日期,假设所有对 post 的回复都将在原始 post.
之后发生
我有 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 to Content 2 | 2016-09-04
5 | Reply to Content 1 | 2016-09-05
6 | Reply to Content 2 | 2016-09-03
Table: REPLY_COMMENTS
---------------------------------
COMMENT_ID | REPLY_TO_COMMENT_ID
---------------------------------
4 | 2
5 | 1
6 | 2
我想显示这样的记录:
---------------------------------------------
COMMENT_ID | CONTENT | CREATE_DATE
---------------------------------------------
1 | Content 1 | 2016-09-01
5 | Reply to Content 1 | 2016-09-05
2 | Content 2 | 2016-09-02
6 | Reply to Content 2 | 2016-09-03
4 | Reply to Content 2 | 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)。
我 只想使用 COMMENTS.CREATE_DATE
字段 - 不想使用 COMMENT_ID
字段,因为它是主键(甚至可能吗? ).
SELECT t1.COMMENT_ID,
t1.CONTENT,
t1.CREATE_DATE
FROM COMMENTS t1
LEFT JOIN REPLY_COMMENTS t2
ON t1.COMMENT_ID = t2.COMMENT_ID
ORDER BY COALESCE(t2.REPLY_TO_COMMENT_ID, t1.COMMENT_ID),
t1.CREATE_DATE
解释:
ORDER BY
子句使用两个术语进行排序。第一个 COALESCE
项将 return parent 消息的 COMMENT_ID
(对于 parent 和单个后代 children)。这样做的原因是对于 children 它将使用连接的 ID,对于 parents,找到 NULL 它也将默认为 parent ID。第二个排序项使用创建日期,假设所有对 post 的回复都将在原始 post.