简体   繁体   English

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

[英]PHP/MySQL: Retrieve a single path in the Adjacency List model

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? 有没有什么有效的方法可以在不限制深度的情况下根据节点的ID检索邻接列表模型中的单个路径? Like if I've got an ID for a node named "Banana" I could get the following Path: Food > Fruits > Banana 就像如果我有一个名为“香蕉”的节点的ID一样,我可以得到以下路径:食物>水果>香蕉

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? 如果不可能的话,这不是一个大问题,但是我考虑过是否可以通过while循环或其他方式运行联接? Until the parent turns 0. 直到父母变成0。

No, not in MySQL at least. 不,至少不是在MySQL中。 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. 一些DBMS(例如SQL Server 2005,Postgres 8.4和Oracle 11g)支持使用带有WITH关键字的常用表表达式来进行递归查询。 This feature allows queries such as this to be written with ease, but unfortunately MySQL does not support recursive queries yet. 这项功能使编写这样的查询变得容易,但是不幸的是,MySQL还不支持递归查询。

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: 您可能有兴趣查看下面的文章,该文章描述了替代模型( 嵌套集合模型 ),该模型使递归操作在MySQL中更容易(可能):

In addition, I also suggest checking out the following presentation by @Bill Karwin , a regular contributor on Stack Overflow: 另外,我还建议您查看Stack Overflow的定期撰稿人@Bill Karwin的以下演示文稿:

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 ). 他在他的《 SQL Antipatterns》一书中更详细地描述了该模型( 摘自该主题的章节 )。

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

No, MySQL doesn't have recursive queries like PostgreSQL, Oracle or SQL Server. 不,MySQL没有像PostgreSQL,Oracle或SQL Server这样的递归查询。 The adjacency list model isn't a great model when using MySQL, a nested set is a better (but more complex) one. 当使用MySQL时,邻接表模型不是一个很好的模型,嵌套集是一个更好的(但更复杂)的模型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM