【发布时间】:2013-09-08 20:12:22
【问题描述】:
问题本身可能写错了,所以如果有人知道如何定义它,请编辑问题。
结构
仅重要的列。
cmets
- comment_id
- comment_parent_id
- user_id
- comment_text
帖子
- post_id
- user_id
- post_text
cmets_to_post (cmets_to_foos, cmets_to_bars, ...)
- comment_id
- post_id (foo_id, bar_id, ...)
comments_to_post 与 cmets 的连接只有在 comments 上的 comment_parent_id 为 NULL 时,因为所有其他评论都只是其他评论的子项。
我正在努力处理查询,结果将给我分配到特定帖子(post_id)的 cmets 及其子项。
我尝试从comments 中选择并加入comments_to_post 其中post_id = ? 但难怪我只有带有NULL comment_parent_id 的cmets,因为只有它们通过comments_to_post 连接。
如何在结果中添加子项 cmets?
示例数据:
cmets
comment_id comment_parent_id
1 NULL
2 1
3 1
4 NULL
5 4
帖子
post_id
10
20
cmets_to_posts
comment_id post_id
1 10
4 20
预期结果
... where post_id = 10
comment_id
1
2
3
【问题讨论】:
-
向我们展示一些相关数据和您想要的结果。
-
我会在 cmets 表中添加一个 post_id 字段,以便您可以删除 cmets_to_post 表。它使您免于递归搜索...
-
@MartyMcVry,那么我应该为我想要有 cmets 的每个表添加列吗?在实际示例中,这将是 5 列。
-
你确定你需要在 cmets 上建立一个树层次结构吗?拥有一个
post_comment_number会更容易,它将为每个帖子的第 1、2、3、... 评论分配编号 1、2、3、...。除非您也需要 cmets 发表评论。 -
@ypercube,是的,我确定。我喜欢在 YouTube 上呈现 cmets 的想法。列出您回复的评论的可能性。
标签: mysql