postgresql中实现oracle SYS_CONNECT_BY_PATH
2024-08-25 00:02:25
oracle:
select sys_connect_by_path(t2.ID, '/') as PATH, t1.id
from HTD_DEVICETYPE_RELATION t1
inner join HTD_DEVICETYPE_RESOURCE t2 on t1.CHILD_RESOURCE_TYPE_ID = t2.ID
start with t1.parent_resource_type_id = 100 and t1.id > 1 connect by nocycle
prior t2.id = t1.parent_resource_type_id order by t1.id
postgresql:
with recursive r_table(path,cycle,t1id,t2id,parent_resource_type_id) as
(
select array[t.t2id],false,t.t1id,t.t2id,parent_resource_type_id from
(select t1.id as t1id,t2.id as t2id,t1.parent_resource_type_id as parent_resource_type_id
from HTD_DEVICETYPE_RELATION as t1
inner join HTD_DEVICETYPE_RESOURCE as t2 on
t1.CHILD_RESOURCE_TYPE_ID = t2.ID where t1.parent_resource_type_id = 100 and t1.id > 1) as t
union all
select path || b.t2id,b.t2id = any(path),b.t1id,b.t2id,b.parent_resource_type_id
from r_table
inner join
(select t1.id as t1id,t2.id as t2id,t1.parent_resource_type_id as parent_resource_type_id
from HTD_DEVICETYPE_RELATION as t1
inner join HTD_DEVICETYPE_RESOURCE as t2 on
t1.CHILD_RESOURCE_TYPE_ID = t2.ID) as b
on r_table.t2id = b.parent_resource_type_id and not cycle
)select '/' || Array_to_string(path,'/'),t1id from r_table where not cycle order by t1id; ?column? | t1id
--------------------------+------
/31 | 9
/31/32 | 10
/102 | 1519
/103 | 1520
/104 | 1521
/105 | 1522
/102/115 | 1523
/102/160 | 1524
/102/103/115 | 1526
/103/115 | 1526
/103/160 | 1527
/102/103/160 | 1527
/102/103/113 | 1528
/103/113 | 1528
/104/160 | 1530
/102/104/160 | 1530
/102/105/115 | 1531
/105/115 | 1531
/102/105/160 | 1532
/105/160 | 1532
/102/105/113 | 1533
/105/113 | 1533
/484 | 1534
/491 | 1554
/102/104/114 | 1556
/104/114 | 1556
/185 | 1596
/154 | 1614
/493 | 1634
/504 | 1635
--More--
实例中通过array数值保存访问过的id,b.t2id = any(path)检查是否已经访问过来避免产生死循环
在递归查询时,如出现如下错误
是由于数据库表字段类型numeric(20,0)不支持with递归查询,将数据库表字段改为bigint即可。
最新文章
- webpack和gulp的区别
- Jmeter组件2. Timer 定时器
- Dynamic CRM2016在一台本地服务器安装部署
- wpf msdn在线地址http://msdn.microsoft.com/zh-cn/library/ms752324(v=vs.110).aspx
- 【python】字符串变量赋值时字符串可用单或双引号
- Java 反射和内省实现spring的IOC和DI
- Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统
- Ubuntu 16.04 系统无法挂载u盘的问题
- python第一百三十天 ---简单的BBS论坛
- sale.order
- mql初学事物和视图
- springboot+mybatis+freemark+oauth开发环境搭建
- shiro简单学习的简单总结
- /storage/xx-xx/, /sdcard, /mnt/sdcard 三者的区别
- python学习 day11 (3月16日)----(生成器内置函数)
- LintCode——旋转字符串
- [BUG随想录] 看不见的分隔符: Zero-width space
- 【bzoj 1076】【SCOI2008】奖励关
- ReactNative调用aar文件(附:如何打开、查看aar文件内容)
- phantomJs页面截图
热门文章
- keystone验证安装
- Java——ArrayList底层源码分析
- 《剑指offer》面试题22 栈的压入、弹出序列 Java版
- sudo pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple ipython
- springboot2.0-统一处理返回结果和异常情况
- neo4j源码分析1-编译打包启动
- 14-jquery元素节点操作
- 02CSS
- Educational Codeforces Round 68 (Rated for Div. 2) D. 1-2-K Game (博弈, sg函数,规律)
- 美国Science公布:全球125个最前沿的科学难题(图)