mysql数据库如何存储分层数据?

发布于 2025-01-09 10:49:59 字数 443 浏览 3 评论 0原文

我现在以 JSON 形式保存未知数量分支的数据,子级可以有未知数量的子级。

example1 - {employee_1:{employee_2:{employee_3:{}}}}
example2 - {employee_3:{employee_2:employee_1:{}}, employee_4:{employee_3:{}}}

示例层次结构

现在要获取此数据,我必须首先获取保存为 json 的层次结构,然后获取存储在该 json 中的 id

有没有办法在一个查询中获取这个?
或者有更好的方法来存储和检索它吗?

我正在使用 MySQL

I am saving data with unknown number of branches in JSON form for now and children can have unknown number of children.

example1 - {employee_1:{employee_2:{employee_3:{}}}}
example2 - {employee_3:{employee_2:employee_1:{}}, employee_4:{employee_3:{}}}

example hierarchical structure

Now to fetch this data, i have to first fetch this hierarchy saved as json and then fetch the id's stored in this json

is there any way to fetch this in one query?
or is there better approach to store and retrieve this?

i am using MySQL

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

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

发布评论

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

评论(1

会傲 2025-01-16 10:49:59

员工表

idname
1Alice
2Bob
3Emily
4Brian

示例 1 员工关系表

idparent_idchild_id
123
212

示例 2 员工关系表

id父 ID 子ID
143
221
334
432

Employee Table

idname
1Alice
2Bob
3Emily
4Brian

Example 1 Employee Relation Table

idparent_idchild_id
123
212

Example 2 Employee Relation Table

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