postgresql中实现oracle SYS_CONNECT_BY_PATH
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即可。
postgresql中实现oracle SYS_CONNECT_BY_PATH的更多相关文章
- 如何在postgresql中模拟oracle的dual表,来测试数据库最基本的连接功能?
还好,网上弄到的,,没有dual的数据库,可以试图用select函数不带from数据表的方式来实现返回值. 一段测试代码: try: conn = psycopg2.connect(database= ...
- 在PostgreSQL中使用oracle_fdw访问Oracle
本文讲述如何在PostgreSQL中使用oracle_fdw访问Oracle上的数据. 1. 安装oracle_fdw 可以参照:oracle_fdw in github 编译安装oracle_fdw ...
- Postgresql中临时表(temporary table)的特性和用法
熟悉Oracle的人,相比对临时表(temporary table)并不陌生,很多场景对解决问题起到不错的作用,开源库Postgresql中,也有临时表的概念,虽然和Oracle中临时表名字相同,使用 ...
- Postgresql中的数据类型大全
一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...
- Postgresql中的large object
1.初识postgresql large object 一位同事在对使用pg_dump备份出来的文件(使用plain格式)进行恢复时,觉得速度非常慢,让我分析一下是什么原因. 我拿到他的.bak文件, ...
- 用ArcMap在PostgreSQL中创建要素类需要执行”create enterprise geodatabase”吗
问:用Acmap在PostgreSQL中创建要素类需要执行"create enterprise geodatabase"吗? 关于这个问题,是在为新员工做postgresql培训后 ...
- PostgreSQL中定时job执行(pgAgent)
PostgreSQL中定时job执行 业务分析 近期项目需要定期清理数据库中的多余数据,即每月1号删除指定表中一年以上的数据. 初步分析这种定时job可以使用一下两种技术实现: Linux的cront ...
- 通过arcgis在PostgreSQL中创建企业级地理数据库
部署环境: Win7 64位旗舰版 软件版本: PostgreSQL-9.1.3-2-windows-x64 Postgis-pg91x64-setup-2.0.6-1 Arcgis 10.1 SP1 ...
- 在64位SQL Server中创建Oracle的链接服务器
当我们同时使用SQL Server和Oracle来存储数据时,经常会用到跨库查询.为了方便使用跨库查询,一个最好的办法就是通过创建链接服务器来实现.既可以在SQL Server中创建Oracle的链接 ...
随机推荐
- linux上执行mysql的脚本文件
我们测试过程中,经常需要执行升级脚本或导入生产测试数据,对于轻量的升级脚本可以直接在客户端工具中打开执行,但是对于文件内容比较大的.sql文件,比如几百M,几G的sql文件,直接拖到客户端工具打开执行 ...
- layui动态渲染select等组件并初始化赋值失败
描诉:有一个用户信息form表单,其中有部门单选框,数据库中有一张dept(部门)表,要动态渲染出所有部门,并默认选中用户所在部门 关键代码: html页面 <div class="l ...
- ThinkPHP关联模型详解
在ThinkPHP中,关联模型更类似一种mysql中的外键约束,但是外键约束更加安全,缺点却是在写sql语句的时候不方便,ThinkPHP很好得解决了这个问题.但是很多人不动关联模型的意思.现在就写个 ...
- 数塔 Easy
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题 ...
- HDUSTOJ-1559 Vive la Difference!(简单题)
1559: Vive la Difference! 时间限制: 3 Sec 内存限制: 128 MB提交: 18 解决: 14[提交][状态][讨论版] 题目描述 Take any four po ...
- vue-split-table【表格合并和编辑插件】
前言 vue-split-table应用的效果图 vue-split-table开源地址,欢迎star,现在已经开源和同步到npm上轻松搞定表格拆分或者合并,编辑,再也不怕被产品怼啦 1.核心源码分析 ...
- px-em-pt等字体的不同
- 该项目不知道如何运行配置文件 IIS Express。The project doesn’t know how to run the profile IIS Express
原文:该项目不知道如何运行配置文件 IIS Express. 方案1(推荐). 可能原因是:禁用掉Microsft ASP.NET和Web工具扩展和微软Azure的应用程序服务工具扩展,恢复启用即可. ...
- Laravel 学习笔记之文件上传
自定义添加磁盘——upload 位置:config/filesystems.php 'disks' => [ 'local' => [ 'driver' => 'local', 'r ...
- dom的节点操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...