简体   繁体   English

SQL:获取嵌套集中节点的直接下属

[英]SQL: Getting immediate subordinates of node in nested set

I'm using this article as a guide to create a database tree structure using the nested set model. 我将本文作为指导来使用嵌套集模型创建数据库树结构。 However, I can't get it to work: when I execute the following query: 但是,我无法使其工作:执行以下查询时:

SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
        nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;

I get this error: 我收到此错误:

#1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and
contains nonaggregated column 'test.node.lft' which is not
functionally dependent on columns in GROUP BY clause; this is 
incompatible with sql_mode=only_full_group_by

Someone in the comments of that article suggested to group by ID instead of name, so I replaced GROUP BY node.name by GROUP BY node.category_id and that works. 那篇文章的评论中有人建议按ID而不是名称进行分组,因此我用GROUP BY node.name替换了GROUP BY node.name ,并且GROUP BY node.category_id

However, some of the other queries in the article still don't work. 但是,本文中的其他一些查询仍然不起作用。 I'd like to select the immediate children of a node, for example: 我想选择一个节点的直接子级,例如:

SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
        nested_category AS parent,
        nested_category AS sub_parent,
        (
                SELECT node.name, (COUNT(parent.name) - 1) AS depth
                FROM nested_category AS node,
                        nested_category AS parent
                WHERE node.lft BETWEEN parent.lft AND parent.rgt
                        AND node.name = 'PORTABLE ELECTRONICS'
                GROUP BY node.name
                ORDER BY node.lft
        )AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
        AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
        AND sub_parent.name = sub_tree.name
GROUP BY node.name
HAVING depth <= 1
ORDER BY node.lft;

But I can't get it to work, because I have no idea how it works (nor where to start). 但是我无法使其工作,因为我不知道它是如何工作的(也不知道从哪里开始)。 Could someone please explain how to understand these queries, maybe step by step? 有人可以逐步解释如何理解这些查询吗?

I'm using MySQL Ver 14.14 Distrib 5.7.17. 我正在使用MySQL Ver 14.14 Distrib 5.7.17。

Thanks a lot! 非常感谢!
Pieter 彼得

Hi I am using the same query and it is working fine for me. 嗨,我正在使用相同的查询,它对我来说很好。

SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
    nested_category AS parent,
    nested_category AS sub_parent,
    (
            SELECT node.name, (COUNT(parent.name) - 1) AS depth
            FROM nested_category AS node,
                    nested_category AS parent
            WHERE node.lft BETWEEN parent.lft AND parent.rgt
                    AND node.id= 36
            GROUP BY node.name
            ORDER BY node.lft
    )AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
    AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
    AND sub_parent.name = sub_tree.name
GROUP BY node.name
HAVING depth = 1
ORDER BY node.lft;

Instead of node.name is used node.id and changed HAVING depth <= 1 to = 1, since I don't want to display the head node. 由于我不想显示头节点,因此使用node.id代替了node.name,并将HAVING depth <= 1更改为= 1。

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

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