PHP/MySQL:检索邻接列表模型中的单个路径

发布于 2024-09-18 03:42:57 字数 176 浏览 2 评论 0原文

有没有什么有效的方法可以在不限制深度的情况下根据节点的ID检索邻接列表模型中的单个路径?就像如果我有一个名为“Banana”的节点的 ID,我可以获得以下路径: Food > >水果> Banana

如果不可能的话也不是什么大问题,但我想是否可以通过 while 循环或其他方式运行连接?直到父母变成0。

Is there any effective way to, without limiting the depth, retrieve a single path in a Adjacency List model based on the node's ID? Like if I've got an ID for a node named "Banana" I could get the following Path: Food > Fruits > Banana

It's not a big problem if it's impossible, but I thought about if it could be possible to run joins through a while-loop or something? Until the parent turns 0.

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

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

发布评论

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

评论(3

九八野马 2024-09-25 03:42:57

不,至少在 MySQL 中不是。这是邻接列表模型的最大限制之一。

您可以继续自加入有限次数,但这很丑陋、尴尬,而且不能覆盖无限的部门。您还可以下载应用程序中的所有数据,构建树,并在应用程序中查找路径。

某些 DBMS,例如 SQL Server 2005、Postgres 8.4 和 Oracle 11g,支持使用公用表表达式 与 WITH 关键字。这个特性允许轻松编写这样的查询,但不幸的是 MySQL 还不支持递归查询。

您可能有兴趣查看以下文章,其中描述了另一种模型(嵌套集合模型),这使得 MySQL 中的递归操作变得更容易(可能):

此外,我还建议您查看 @Bill Karwin 的以下演示文稿Stack Overflow 上的定期撰稿人:

演示中描述的闭包表模型是嵌套集的一个非常有效的替代方案。他在他的 SQL Antipatterns 一书中更详细地描述了该模型 (摘自该主题的章节)。

No, not in MySQL at least. That is one of the biggest limitations of the Adjacency List Model.

You could continue to self join a finite number of times, but that's ugly, awkward and doesn't cover unlimited dept. You could also download all the data in your application, build a tree, and find the path in the application.

Some DBMSes, such as SQL Server 2005, Postgres 8.4 and Oracle 11g, support recursive queries using common table expressions with the WITH keyword. This feature allows queries such as this to be written with ease, but unfortunately MySQL does not support recursive queries yet.

You may may be interested in checking out the following article which describes an alternative model (the nested set model), which makes recursive operations easier (possible) in MySQL:

In addition, I also suggest checking out the following presentation by @Bill Karwin, a regular contributor on Stack Overflow:

The closure table model described in the presentation is a very valid alternative to the nested set. He describes this model in further detail in his SQL Antipatterns book (excerpt from the chapter on this topic).

千里故人稀 2024-09-25 03:42:57

不,MySQL 没有像 PostgreSQL、Oracle 或 SQL Server 那样的递归查询。使用 MySQL 时,邻接列表模型并不是一个很好的模型,嵌套集是更好(但更复杂)的模型。

http://mikehillyer.com/articles/managing-hierarchical-data-in -mysql/

No, MySQL doesn't have recursive queries like PostgreSQL, Oracle or SQL Server. The adjacency list model isn't a great model when using MySQL, a nested set is a better (but more complex) one.

http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

九公里浅绿 2024-09-25 03:42:57

尝试这个查询:

SET @id:=12345;

SELECT content_name, content_id, (@id:=content_parent) as content_parent 
FROM 
    ( SELECT content_id, content_name, content_id, content_parent 
      FROM content_table 
      ORDER BY content_parent DESC
    ) AS aux_table 
    WHERE content_id = @id

Try this query:

SET @id:=12345;

SELECT content_name, content_id, (@id:=content_parent) as content_parent 
FROM 
    ( SELECT content_id, content_name, content_id, content_parent 
      FROM content_table 
      ORDER BY content_parent DESC
    ) AS aux_table 
    WHERE content_id = @id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文