sql递归

递归向下查询是用 虚拟表的id(e3)去联结 递归表的parent_id(ccc1)
递归向上查询是用 虚拟表的parent_id(e3)去联结 递归表的id(ccc1)
WITH RECURSIVE le (catalog_id,
catalog_name,
catalog_type,
parent_id,
hos_id,
catalog_doc_code,
catalog_dept_code,
catalog_state) as
(
select ccc.id as catalog_id,
ccc.name as catalog_name,
ccc.catalog_type as catalog_type,
ccc.parent_id as parent_id,
ccc.hos_id as hos_id,
ccc.doc_code as catalog_doc_code,
ccc.dept_code as catalog_dept_code,
ccc.state as catalog_state
from his.cli_combine_catalog ccc
where ccc.hos_id = #{hosId}
and ccc.state = ‘1’
and ccc.id = #{id}
union all

select ccc1.id as catalog_id,
ccc1.name as catalog_name,
ccc1.catalog_type as catalog_type,
ccc1.parent_id as parent_id,
ccc1.hos_id as hos_id,
ccc1.doc_code as catalog_doc_code,
ccc1.dept_code as catalog_dept_code,
ccc1.state as catalog_state
from his.cli_combine_catalog ccc1,
le e3
where e3.catalog_id = ccc1.parent_id and ccc1.state=‘1’
)
select *
from le;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值