gpt4 book ai didi

mysql - 在 SQL 树结构中查找节点

转载 作者:行者123 更新时间:2023-11-29 05:31:07 24 4
gpt4 key购买 nike

我有一个存储在 MYSQL 数据库中的树状数据模型。通过在每个“节点”对象上使用两个字段来遍历树:

  • 编号
  • 父代号

根 id 的 parent_id 为“null”,树中的所有其他节点都引用父 id。

如果我想获得此树中所有节点 ID 的列表,我必须递归遍历树并收集每个节点的一个或多个属性,使用本质上是包含许多查询的 for 循环。

我想知道是否有一种方法可以通过 SQL 中的 on 查询更有效地完成这一切

最佳答案

我写了一篇关于我称为 Closure Table 的解决方案的高评价帖子:What is the most efficient/elegant way to parse a flat table into a tree?

我还在我的演示文稿中介绍了该设计 Models for Hierarchical Data with SQL and PHP在我的书中 SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming .

而且我在 Stack Overflow 上多次回答了有关分层数据查询的问题: https://stackoverflow.com/search?q=user%3A20860+%5Bhierarchical-data%5D

Quassnoi写了一系列关于在 MySQL 中查询树的史诗系列博客文章: http://explainextended.com/2009/07/22/hierarchial-queries-in-mysql-identifying-trees/

Quassnoi 还在 Stack Overflow 上回答了一些关于分层数据的问题: https://stackoverflow.com/search?q=user%3A55159+%5Bhierarchical-data%5D

关于mysql - 在 SQL 树结构中查找节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14656573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com