通过不唯一的名称选择嵌套集模型中的节点

发布于 2024-09-24 19:44:05 字数 1132 浏览 3 评论 0原文

我正在尝试管理 嵌套集模型表,不是通过唯一ID,而是通过名称(字符串),并且树内不同父级下的其他节点可以以相同的方式调用。

到目前为止,我使用唯一的 ID 来获取嵌套集合内的节点:

SELECT
     node.name, node.lft, node.rgt
FROM tbl AS parent, tbl AS node
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = '{$node_id}'
GROUP BY node.id

尝试将此方法扩展为通过名称检索节点的更通用方法,我提出了一个查询,其中包含与深度相同的 HAVING 子句要检索的节点,检查节点名称及其深度:

SELECT
     node.name, node.lft, node.rgt, COUNT(node.id) AS depth
FROM tbl AS parent, tbl AS node
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.id
HAVING
     (node.name = 'myParentName' AND depth = 1)
     OR
     (node.name = 'myParent2Name' AND depth = 2)
     OR
     ...
 # and so on

但它并不完美:有两个具有相同名称和相同深度的节点,但在不同的父节点中,无论它们属于哪个层次结构,都将被检索。

示例:

ARTICLES
   |
   +--PHP
   |   +--the-origins
   |   +--syntax
   +--JS
       +--history
       +--syntax

在这种情况下,上面的查询将返回 ARTICLES/PHP/syntax 或 ARTICLES/JS/syntax:深度为 3 的“syntax”节点实际上位于 PHP 节点下或 JS 节点下。 有没有一条有效的路径可以走,来解决这个问题呢?

I am trying to manage the retrieval of a node in a nested set model table, not through the unique ID, but through the name (a string), and other nodes within the tree under different parents may be called the same way.

As far as now I used an unique ID to get nodes inside the nested sets:

SELECT
     node.name, node.lft, node.rgt
FROM tbl AS parent, tbl AS node
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = '{$node_id}'
GROUP BY node.id

Trying to extend this method to a more general way to retrieve the node through its name, I came up with a query containing as much HAVING clauses as the depth of the node to retrieve, checking for the node name and its depth:

SELECT
     node.name, node.lft, node.rgt, COUNT(node.id) AS depth
FROM tbl AS parent, tbl AS node
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.id
HAVING
     (node.name = 'myParentName' AND depth = 1)
     OR
     (node.name = 'myParent2Name' AND depth = 2)
     OR
     ...
 # and so on

But it is not perfect: having two nodes with the same name and the same depth, but within different parents, both are retrieved, no matter the hierarchy they belong to.

Example:

ARTICLES
   |
   +--PHP
   |   +--the-origins
   |   +--syntax
   +--JS
       +--history
       +--syntax

In this case, the query above would return either ARTICLES/PHP/syntax or ARTICLES/JS/syntax: a "syntax" node with depth 3, infact, is either under the PHP node or under the JS node.
Is there an effective path to walk, to solve this problem?

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

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

发布评论

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

评论(1

只是我以为 2024-10-01 19:44:05

我不太确定你想在这里做什么。您是否正在尝试访问路径名为 ARTICLES/PHP/syntax 的节点?如果是这样,您需要做的是每个父级别的自连接:

SELECT n0.*
FROM tbl AS n0
JOIN tbl AS n1 ON n0.lft BETWEEN n1.lft+1 AND n1.rgt
JOIN tbl AS n2 ON n1.lft BETWEEN n2.lft+1 AND n2.rgt
WHERE n0.name='syntax' AND n1.name='PHP' AND n2.name='ARTICLES'

ETA 评论:在嵌套集中进行直接子匹配并不是很有趣。您必须尝试在每个连接行之间选择一个中间父行。这是您不希望存在的行,因此您可以使用空左连接来反转该条件。例如。:

SELECT n0.*
FROM tbl AS n0
JOIN tbl AS n1 ON n0.lft BETWEEN n1.lft+1 AND n1.rgt
JOIN tbl AS n2 ON n1.lft BETWEEN n2.lft+1 AND n2.rgt
LEFT JOIN tbl AS nn1 ON nn1.lft BETWEEN n1.lft+1 AND n0.lft-1
LEFT JOIN tbl AS nn2 ON nn2.lft BETWEEN n2.lft+1 AND n1.lft-1
WHERE nn1.lft IS NULL AND nn2.lft IS NULL
AND n0.name='syntax' AND n1.name='PHP' AND n2.name='ARTICLES'

I'm not quite sure what you're trying to do here. Are you trying to access the node with the pathname ARTICLES/PHP/syntax? If so what you'd need to do would be a self-join for each parent level:

SELECT n0.*
FROM tbl AS n0
JOIN tbl AS n1 ON n0.lft BETWEEN n1.lft+1 AND n1.rgt
JOIN tbl AS n2 ON n1.lft BETWEEN n2.lft+1 AND n2.rgt
WHERE n0.name='syntax' AND n1.name='PHP' AND n2.name='ARTICLES'

ETA re comments: doing direct-child matches in nested set isn't much fun. You'd have to try to select an intermediate parent row between each joined row. This is the row you don't want to exist, so you then invert that condition with a null left join. eg.:

SELECT n0.*
FROM tbl AS n0
JOIN tbl AS n1 ON n0.lft BETWEEN n1.lft+1 AND n1.rgt
JOIN tbl AS n2 ON n1.lft BETWEEN n2.lft+1 AND n2.rgt
LEFT JOIN tbl AS nn1 ON nn1.lft BETWEEN n1.lft+1 AND n0.lft-1
LEFT JOIN tbl AS nn2 ON nn2.lft BETWEEN n2.lft+1 AND n1.lft-1
WHERE nn1.lft IS NULL AND nn2.lft IS NULL
AND n0.name='syntax' AND n1.name='PHP' AND n2.name='ARTICLES'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文