将msg记录的父子层次结构保存到mysql中?

发布于 2024-10-05 03:40:27 字数 327 浏览 0 评论 0原文

如何将消息数据存储到数据库中,以便可以将其打印为无序列表中的父子节点?每个根节点可以有 2 个子节点(深度),即像祖父、父亲和父亲。孩子们。例如,

 -vehicle
   -public
     -Lorry //Lorry is lost node for root vehicle,           

当前我存储的有关消息的字段是ID(PK),名称,msg,msg_id(唯一)。我想如果每个记录都可以有一个“父”字段,其中包含其子记录的“msg_id”。然后,该信息可用于检索所有父母和孩子。父节点的所有子节点。然后这些可以打印在父子层次结构中的无序列表中。

How could I store messages data into a database so that I could print it as parent child nodes in an unordered list? Each root node could have 2 child(depth) i.e. like grand father, father, & children. e.g.

 -vehicle
   -public
     -Lorry //Lorry is lost node for root vehicle,           

The fields currently I store about a msg are ID(PK),name,msg,msg_id(unique). I am thinking like if each record could have a 'parent' field that would contain 'msg_id' of the record it is a child of. Then this information could be used to retrieve all parents & all child nodes of parents. And then those could be printed in a parent child hierarchy in an unordered list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-10-12 03:40:27

对于每个节点(数据库中的行)存储对父节点的引用。

示例:

| name    | id      | parent_id
------------------------------
| vehicle | 1       | null
| public  | 2       | 1
| Lorry   | 3       | 2

这是最简单、最直接的方法。

For each node (row in the database) store a reference to the parent.

Example:

| name    | id      | parent_id
------------------------------
| vehicle | 1       | null
| public  | 2       | 1
| Lorry   | 3       | 2

This is the easiest and most straight-forward approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文