您的位置:首页 > 数据库

给一个unitid找到他所有的父系单位id

2013-11-13 19:21 141 查看
实际就是个PostgreSQL的递归运算

with RECURSIVE
cte as
(

select unit_parentid from gpm_unit where id=69                               //循环体(69是已知的最初单位id)
union all 
select gpm_unit.unit_parentid  fromgpm_unit inner
join
cte as con
c.unit_parentid = gpm_unit.id   //循环条件
)
select unit_parentid
from cte;

 

红色字体是 这段语句的骨架,其余的都是要往上添加的肉
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐