查询以获取数据库中树的所有祖先/后代?

发布于 2024-09-14 05:05:44 字数 245 浏览 3 评论 0原文

我有一个表(id,parent_id,data),其中parent_id指向同一个表中的另一行(或者为空)。

是否有一种标准方法来查询(1)某个 id 的所有祖先和(2)某个 id 的所有后代?

我也在 DBIx::Class 中执行此操作,因此如果有一种最方便的方法可以使用该模块(或其他模块)执行此操作,我也很想听听。

编辑:澄清-所有父母=所有祖先,所有孩子=所有后代。

I have a table (id, parent_id, data) where parent_id points to another row in same table (or is null).

Is there a standard way to query (1) all the ancestors of a certain id and (2) all the descendants of a certain id?

I'm also doing this in DBIx::Class, so if there's a most convenient way to do it with that module (or some other), I'd love to hear about that as well.

EDIT: clarify - all parents = all ancestors, all children = all descendants.

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

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

发布评论

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

评论(2

等你爱我 2024-09-21 05:05:44

这很大程度上取决于您所使用的 SQL 风格。

在 Oracle 中,您可以使用 START WITH id = yourid CONNECT BY PRIOR id =parent_id 构造。在 PostgreSQL 中,您可以使用函数 connectby('tablename', 'id', 'parent_id', 'id', value, 0)

在许多情况下,表示树是有意义的
不同的是,通过定义一个列来保存,
对于每个节点,从根元素开始的完整路径
到这个节点。

这种技术有很多例子
在网上找到的,我最近看到的,
它还涉及 DBIx::Class,可以在这里找到: http://blogs.perl.org/users/ ovid/2010/05/threaded-forum-sql.html

This highly depends on the flavor of SQL you are using.

In Oracle, you can use the START WITH id = yourid CONNECT BY PRIOR id = parent_id construct. In PostgreSQL, you can use a function connectby('tablename', 'id', 'parent_id', 'id', value, 0).

In many cases, it makes sense to represent trees
differently, by defining a column which will hold,
for every node, a complete path from the root element
to this node.

There are plenty examples of this technique
to be found on the Internet, the most recent one I saw,
which also deals with DBIx::Class, can be found here: http://blogs.perl.org/users/ovid/2010/05/threaded-forum-sql.html

苍风燃霜 2024-09-21 05:05:44

看来我们要使用 DBIx::Class::Tree::AdjacencyList 此时。它几乎完成了我正在寻找的所有事情(不幸的是,没有祖先结果集 - 但我们可以通过从另一个方向处理我们需要提出的问题来解决这个问题)。

然而,@Grrrr 的回答让我思考,我们可以添加一个单独的表+模块 (id, record_type, record_ancestors) ,它将附加到具有 parent_id 列的模型并提供一个 ancestors 结果集(基本上是通过执行 search_rs ,其中 id 位于由我们选择的 w/e 分隔符分割的相关祖先行中)。为了获得这样的结果集,这需要相当多的工作,因此,如果我们发现询问“这是父项 x 的子项”确实不切实际并且真正需要“这是父项 x 的父项吗?”的问题,我们可能只会去那里。孩子x”?

编辑:或者也许我们会使用 DBIx::Class::Tree::Mobius - 尽管看起来原始表格难以理解。

It looks like we're going to go with DBIx::Class::Tree::AdjacencyList at the moment. It does almost everything I was looking for (no ancestors resultset, unfortunately - but we can work around that by approaching the questions we need to ask from the other direction).

However, @Grrrr's answer got me thinking, and we may add a separate table + module (id, record_type, record_ancestors) that would attach to our models that have a parent_id column and provide an ancestors resultset (basically by doing a search_rs where the id is in the split of the relevant ancestors row by w/e delimiter we pick). That's a fair bit of work just to get such a result set, so we'll probably only go there if we find questions where it's really impractical to ask "is this a child of parent x" and really need "is this a parent of child x"?

EDIT: or maybe we'll use DBIx::Class::Tree::Mobius - though it looks like viewing the table raw would be incomprehensible.

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