什么是嵌套评论背后的算法? [英] What is the algorithm behind nested comments?

查看:150
本文介绍了什么是嵌套评论背后的算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/4278003/how-is-designed-programmed-the-reddit-comment-system">How设计/编程的reddit的评论系统?

我想了解背后reddit的的显示注释算法。评论有什么关系与其子等?它们是如何存储在数据库中?

I want to learn the comment displaying algorithm behind Reddit. How is a comment related with its child and so on? How they are stored in the database?

比方说

comment1
-comment2
--comment3
-comment4
--comment5
--comment6
---comment7
----comment8
comment9

如何显示comment5这是comment4这是注释1后后?这是什么排序背后的想法?而如何将他们的数据库?

How to display comment5 which is after comment4 which is after comment1? What is the idea behind this sequencing? And how to relate them in the database?

推荐答案

AS @Rafe说,实际的存储是pretty的方便,这将是这样的:

AS @Rafe said, the actual storage is pretty easy, it would be something like:

|  id  |   name   | parent |
|  1   | comment1 |    0   |
|  2   | comment2 |    1   |
|  3   | comment3 |    2   |
|  4   | comment4 |    1   |
|  5   | comment5 |    4   |
|  6   | comment6 |    4   |
|  7   | comment7 |    6   |
|  8   | comment8 |    7   |
|  9   | comment9 |    0   |

当然,实际得到的信息,从这个是(可以说)最困难的部分。当然,你可以得到一个注释的孩子们喜欢的东西: SELECT * FROM表,其中母公司=4会给你comment4的所有孩子。但是计数的孩子,列出所有的孩子在等级秩序将是一个有点困难。其他的答案可能会提供更多的信息。

Of course actually getting information from this is (arguably) the hard part. You can of course get the children of a comment with something like: SELECT * FROM table WHERE parent='4' will give you all the children of comment4. But counting children, listing all the children in hierarchical order would be a bit harder. Other answers may provide more information on that.

这篇关于什么是嵌套评论背后的算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆