当前位置:首页>开发>正文

递归查找某一条件为是的上级id,oracle-sql oracle的递归能全部显示上下级关系吗

2023-04-15 17:18:58 互联网 未知 开发

 递归查找某一条件为是的上级id,oracle-sql oracle的递归能全部显示上下级关系吗

递归查找某一条件为是的上级id,oracle-sql

简单做了几条数据,我这应该是没问题,只是不知道拿到你那会怎么样,有问题直接回复
测试数据
create table test
(parent_id int,
id int,
is_rp int,
name varchar2(10))
insert into test values (null,1,1,aa)
insert into test values (1,2,1,bb)
insert into test values (1,3,2,cc)
insert into test values (2,4,1,dd)
insert into test values (3,5,2,ee)
insert into test values (4,6,2,ff) 
commit
执行
with t as
(select test.*,level as lv from test 
start with id = 6 --这个地方为输入id
connect by prior parent_id=id)
select name from t where exists
(select 1 from
(select is_rp,min(lv) lv from t where is_rp=1 group by is_rp) a
where a.is_rp=t.is_rp and a.lv=t.lv)你运行一下看看结果

oracle的递归能全部显示上下级关系吗

可以的
--层次查询,自底到顶
select t.employee_id,t.last_name,t.job_id,t.manager_id,level from employees t
start with t.employee_id=10connect by prior t.manager_id=t.employee_id

select t.empno,t.ename,t.job,t.mgr,level from emp t
start with t.empno=778connect by prior t.mgr=t.empno

--层次查询,自顶到底
select t.employee_id,t.last_name,t.job_id,t.manager_id,level from employees t
start with t.employee_id=10connect by prior t.employee_id=t.manager_id

select t.empno,t.ename,t.job,t.mgr,level from emp t
start with t.empno=778connect by prior t.empno=t.mgr

oracle中SQL递归查询

要看这个“下级人数”是怎么定义的,如果只是记录中PID值是父类ID的人数之和,那么就用with,先按PID做一个统计结果集,然后用原本数据表作为查询目标表,左外连接with的结果集,并将人数相加;
如果是要所有下级的人数之和,则需要要递归函数来计算人数。

oracle递归实现查找整个层级关系

SELECT *
FROM TEST_A A
START WITH COALESCE(A.PARENTID,0) = 0
CONNECT BY PRIOR A.ID = A.PARENTID
ORDER BY A.PARENTID

没有进行测试,你自己执行下看看是不是你想要的结果,希望能帮到你。

mariadb有没有 oracle递归

建用户和授权要用DBA

最简单得建用户:
create user 用户名 identified by 密码

用户解锁 alter user 用户名 account unlock(不解锁无法登陆)

授权用 grant

建完用户首先要授权登陆权限

grant create session to 用户名

授权可以授权给角色和用户
也可以把角色授权给角色和用户

其他得类似 创建表得权限类似如下格式:
grant create table to 用户

sql语句查询员工的上级,在同一张表中

如果是SQL2005以上则可以用CTE递归写法

use tempdb
go
create table #emp
(eid integer not null,
ename varchar(10) not null,
epid integer null,
)

insert into #emp
select 1,A, union all
select 2,me, union all
select 3,bos, union all
select 4,boss,null

select * from #emp

with empcte as
(select epid,(select ename from #emp b where b.eid = a.epid) as name from #emp a where ename = me
union all
select b.epid,(select ename from #emp where eid = b.epid) as name from empcte a join #emp b on a.epid = b.eid
)
select * from empcte where epid is not null

oracle递归查询的问题,如何查询出现机构和其属下所有机构

试试这个

select orgid,fatherorgid,orgname
from gias_org
start with orgid = connect by prior id = fatherorgid

最新文章