【sql】608. 树节点

题目:

力扣

给定一个表 tree,id 是树节点的编号, p_id 是它父节点的 id 。

+----+------+
| id | p_id |
+----+------+
| 1  | null |
| 2  | 1    |
| 3  | 1    |
| 4  | 2    |
| 5  | 2    |
+----+------+
树中每个节点属于以下三种类型之一:

叶子:如果这个节点没有任何孩子节点。
根:如果这个节点是整棵树的根,即没有父节点。
内部节点:如果这个节点既不是叶子节点也不是根节点。
 

写一个查询语句,输出所有节点的编号和节点的类型,并将结果按照节点编号排序。上面样例的结果为:

+----+------+
| id | Type |
+----+------+
| 1  | Root |
| 2  | Inner|
| 3  | Leaf |
| 4  | Leaf |
| 5  | Leaf |
+----+------+
 

解释

节点 '1' 是根节点,因为它的父节点是 NULL ,同时它有孩子节点 '2' 和 '3' 。
节点 '2' 是内部节点,因为它有父节点 '1' ,也有孩子节点 '4' 和 '5' 。
节点 '3', '4' 和 '5' 都是叶子节点,因为它们都有父节点同时没有孩子节点。
样例中树的形态如下:
 

              1
            /   \
                      2       3
                    /   \
                  4       5
 

注意

如果树中只有一个节点,你只需要输出它的根属性。

题解:

1.union

三种情况有重叠的要考虑

# Write your MySQL query statement below
select
    id,
    'Root' as Type
from
    tree
where
    p_id is null
union
select
    id,
    'Inner' as Type
from
    tree
where
    id in (
        select
            p_id
        from
            tree
        where
            p_id is not null
    )
    and p_id is not null
union   
select
    id,
    'Leaf' as Type
from
    tree
where
    id not in (
        select
            p_id
        from
            tree
        where
            p_id is not null
    )
    and p_id is not null    

2.case

三种情况前面的满足后面的就不管了

# Write your MySQL query statement below
select
    id,
    case
        when id = (select id from tree where p_id is null)
            then 'Root'
        when id in (select p_id from tree)
            then 'Inner'
        else 'Leaf'
    end as Type
from
    tree

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在SQL中找到机构的父节点,可以使用递归查询来实现。首先,需要知道要查询的机构的ID。然后,使用递归查询来逐级查找机构的父节点,直到找到根节点(即parentid为0的节点)。下面是一个示例的SQL查询语句: ```sql WITH RECURSIVE tb (id, name, parentid) AS ( SELECT id, name, parentid FROM organization WHERE id = <要查询的机构ID> UNION ALL SELECT c.id, c.name, c.parentid FROM organization c JOIN tb t ON c.id = t.parentid ) SELECT * FROM tb WHERE parentid = 0; ``` 其中,`organization`是机构表的名称,`id`、`name`和`parentid`是表中的列名。 使用以上的SQL查询语句,可以找到指定机构的所有父节点,直到根节点。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SQL的递归查询子/父节点结构(MySQL)](https://blog.csdn.net/m0_46252893/article/details/129256248)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [Java递归获取节点下的所有节点](https://blog.csdn.net/weixin_39903375/article/details/114134751)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值