计算 MySQL 中父子模型的深度

发布于 2024-07-30 07:45:44 字数 69 浏览 10 评论 0原文

MySQL下父子模型如何计算节点深度?

我需要深度,除其他外,在我的列表中创建缩进(用 PHP 编码)。

How do I calculate a node's depth in a parent-child model under MySQL?

I'll need the depth to, among other things, create the indent in my list (coded with PHP).

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

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

发布评论

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

评论(3

故事与诗 2024-08-06 07:45:44

这可能是一个老问题,但我只是想让其他人知道我几个月前找到了解决方案。 我最近确实在这里写过:http://en。 someotherdeveloper.com/articles/adjacency-list-model-with-深度-calculation/

This might be an old question, but I just want to let others know that I found a solution some months ago. I did recently write about it here: http://en.someotherdeveloper.com/articles/adjacency-list-model-with-depth-calculation/

灵芸 2024-08-06 07:45:44

如果你只想复制粘贴,这里是我的例子。
我有包含 ID 和 PARENT_ID 字段的表项目。

DELIMITER $
DROP FUNCTION IF EXISTS `getDepth` $
CREATE FUNCTION `getDepth` (project_id INT) RETURNS int
BEGIN
    DECLARE depth INT;
    SET depth=1;

    WHILE project_id > 0 DO
        SELECT IFNULL(parent_id,-1) 
        INTO project_id 
        FROM ( SELECT parent_id FROM Projects WHERE id = project_id) t;

        IF project_id > 0 THEN
            SET depth = depth + 1;
        END IF;

    END WHILE;

    RETURN depth;

END $
DELIMITER ;

If you want just to copy paste here is my example.
I have table Projects with ID and PARENT_ID fileds.

DELIMITER $
DROP FUNCTION IF EXISTS `getDepth` $
CREATE FUNCTION `getDepth` (project_id INT) RETURNS int
BEGIN
    DECLARE depth INT;
    SET depth=1;

    WHILE project_id > 0 DO
        SELECT IFNULL(parent_id,-1) 
        INTO project_id 
        FROM ( SELECT parent_id FROM Projects WHERE id = project_id) t;

        IF project_id > 0 THEN
            SET depth = depth + 1;
        END IF;

    END WHILE;

    RETURN depth;

END $
DELIMITER ;
別甾虛僞 2024-08-06 07:45:44

这取决于数据库中层次结构的实际实现。 如果您使用嵌套集模型(http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/)您可以通过单个选择检索完整的父子路径。

更新:好的,由于您要使用邻接列表模型,我建议在表中存储节点级别。 它不仅会在一个查询中为您提供节点深度,而且还允许您在一个查询中检索到该节点的完整路径(尽管该查询必须动态生成):

SELECT n1.name AS lvl1, n2.name as lvl2, n3.name as lvl3, ..., nN.name as lvlN
  FROM nodes AS n1
  JOIN nodes AS n2 ON n2.parent_id = n1.id
  JOIN nodes AS n3 ON n3.parent_id = n2.id
  ...
  JOIN nodes AS nN ON nN.parent_id = n(N-1).id
WHERE nN.id = myChildNode;

因为您知道您的节点处于开启状态N 级不需要左连接,并且在 id /parent_id 上给出适当的索引,这应该相当快。
这种方法的缺点是,您必须在节点移动期间保持节点级别更新,但这应该相当简单和快速,因为您只会对节点本身及其子节点执行此操作 - 而不是对表的大部分进行操作,如你会用嵌套集来做。

That depends on the actual implementation of your hierarchy in the database. If you are using nested sets model (http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) you can retrieve the full parent-to-child path via a single select.

Update: Ok, since you're going with adjacency list model I suggest to store node level in the table. Not only will it give you the node depth in one query, but it will also allow you to retrieve the entire path to that node in one query (albeit that query would have to be dynamically generated):

SELECT n1.name AS lvl1, n2.name as lvl2, n3.name as lvl3, ..., nN.name as lvlN
  FROM nodes AS n1
  JOIN nodes AS n2 ON n2.parent_id = n1.id
  JOIN nodes AS n3 ON n3.parent_id = n2.id
  ...
  JOIN nodes AS nN ON nN.parent_id = n(N-1).id
WHERE nN.id = myChildNode;

Since you know that your node is on level N there's no need for left joins and, given appropriate indexes on id / parent_id this should be reasonably fast.
The downside to this approach is that you'll have to keep node level updated during node moves, but that should be reasonably straightforward and fast as you would only do it for the node itself and its children - not for the majority of the table as you would do with nested sets.

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