sql查询子节点或父节点

oracle使用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

查询子节点是根据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查询

猜你喜欢

转载自blog.csdn.net/lyf_ldh/article/details/81134767