sql查询子节点或父节点

oracle使用

select *
        from (select st.*
        from (select t.*
        from bas_basic_tree t
        connect by prior t.pid = t.id
        start with t.dataid in
        (select orgid
        from bas_basic_organization_user
        where userid = #{userid})) st)

mysql使用递归函数

通用的是表中增加一个treecode字段用于记录节点信息:

一级节点:001

二级节点:001001,001002

三级节点:001001001,001002001

sql : select substr(concat(1,treecode)+1,2) from bas_model_tree 
        where pid=#{pid} order by treecode desc limit 1

查询子节点是根据treecode模糊查询先查询出id,再根据id查询

查询父节点根据treecode

        String str = treecode;
        String con="(";
        for(int i=0;i<str.length();i+=3) {
            
            con=con+str.substring(i)+",";
        }
        con=con.substring(0, con.length()-1)+")";
 根据con使用in查询出id,再根据id查询

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值