postgresql with递归
在PostgreSQL里,with子句提供了一种方法写一个大的查询中使用的辅助报表与查询。它有助于打破复杂和大型查询简单易读的形式。
1. 建表
- postgres=# create table tb9(id serial primary key,name character varying, parentid integer);
- CREATE TABLE
- postgres=# \d tb9
- Table "public.tb9"
- Column | Type | Modifiers
- ----------+-------------------+--------------------------------------------------
- id | integer | not null default nextval('tb9_id_seq'::regclass)
- name | character varying |
- parentid | integer |
- Indexes:
- "tb9_pkey" PRIMARY KEY, btree (id)
2. 插入测试数据
- postgres=# insert into tb9 values(generate_series(1,5),'john',0);
- INSERT 0 5
- postgres=# insert into tb9 values(6,'john1',1);
- INSERT 0 1
- postgres=# insert into tb9 values(7,'john2',1);
- INSERT 0 1
- postgres=# insert into tb9 values(8,'john11',6);
- INSERT 0 1
- postgres=# select * from tb9;
- id | name | parentid
- ----+--------+----------
- 1 | john | 0
- 2 | john | 0
- 3 | john | 0
- 4 | john | 0
- 5 | john | 0
- 6 | john1 | 1
- 7 | john2 | 1
- 8 | john11 | 6
- (8 rows)
3. with子句
- postgres=# with t as (select * from tb9 where parentid=1) select count(0) from t;
- count
- -------
- 2
- (1 row)
- postgres=# with t(a,b,c) as (select * from tb9 where parentid=1) select a,b,c from t;
- a | b | c
- ---+-------+---
- 6 | john1 | 1
- 7 | john2 | 1
- (2 rows)
4. 多个with子句的结合使用
parentid=1的记录的所有子记录
- postgres=# with t1 as (select * from tb9),t2 as(select * from tb9 where parentid=1) select t1.* from t1,t2 where t2.id=t1.parentid;
- id | name | parentid
- ----+--------+----------
- 8 | john11 | 6
- (1 row)
5. 递归
id为1的记录的所有子记录
- postgres=# with recursive t as(select id,name,parentid from tb9 where id=1 union all select k.id,k.name,k.parentid from tb9 k,t where t.id=k.parentid) select * from t;
- id | name | parentid
- ----+--------+----------
- 1 | john | 0
- 6 | john1 | 1
- 7 | john2 | 1
- 8 | john11 | 6
- 9 | john21 | 7
- (5 rows)
postgresql with递归的更多相关文章
- MVCC PostgreSQL实现事务和多版本并发控制的精华
原创文章,同步发自作者个人博客,http://www.jasongj.com/sql/mvcc/ PostgreSQL针对ACID的实现机制 事务的实现原理可以解读为RDBMS采取何种技术确保事务的A ...
- PostgreSQL中RECURSIVE递归查询使用总结
RECURSIVE 前言 CTE or WITH 在WITH中使用数据修改语句 WITH使用注意事项 RECURSIVE 递归查询的过程 拆解下执行的过程 1.执行非递归部分 2.执行递归部分,如果是 ...
- 一道Postgresql递归树题
转载请注明出处: https://www.cnblogs.com/funnyzpc/p/13698249.html 也是偶然的一次,群友出了一道题考考大家,当时正值疫情最最严重的三月(借口...),披 ...
- PostgreSQL 与 MySQL 相比,优势何在?
一. PostgreSQL 的稳定性极强, Innodb 等引擎在崩溃.断电之类的灾难场景下抗打击能力有了长足进步,然而很多 MySQL 用户都遇到过Server级的数据库丢失的场景——mysql系统 ...
- postgresql查询的处理过程
本文简单描述了Postgresql服务器接收到查询后到返回给客户端结果之间一般操作顺序,总的流程图如下: 第一步: 客户端程序可以是任何符合 PostgreSQL 协议规范的程序,如 JDBC 驱动. ...
- PostgreSQL笔记
本文针对目前最新版9.5.1,若非说明,文中所说文档即指官方文档.本人刚接触PostgreSQL不久,文中不免错漏,请大家指正:随着了解深入,本文[可能]会不定期更新补足. JSON PostgreS ...
- 安装postgreSQL出现configure:error:readline library not found解决方法
要安装 readline , readline-dev 开发包,要么使用 --without-readline 选项关闭 readline 功能. #yum install readline; #yu ...
- 跟我一起读postgresql源码(七)——Executor(查询执行模块之——数据定义语句的执行)
1.数据定义语句的执行 数据定义语句(也就是之前我提到的非可优化语句)是一类用于定义数据模式.函数等的功能性语句.不同于元组增删査改的操作,其处理方式是为每一种类型的描述语句调用相应的处理函数. 数据 ...
- 跟我一起读postgresql源码(八)——Executor(查询执行模块之——可优化语句的执行)
2.可优化语句的执行 可优化语句的共同特点是它们被查询编译器处理后都会生成査询计划树,这一类语句由执行器(Executor)处理.该模块对外提供了三个接口: ExecutorStart.Executo ...
随机推荐
- SIMULINK的模块库介绍
SIMILINK模块库按功能进行分为以下8类子库:Continuous(连续模块)Discrete(离散模块)Function&Tables(函数和平台模块)Math(数学模块)Nonline ...
- Activity 变成对话框,然后再隐藏?
由于继续需要做一些无感操控的工作,之前也记录了下利用悬浮窗的方法.今天突然发现原来activity可以直接嗯嗯嗯啊. 首先我在AndroidManifest里给activity添加了如下一行: and ...
- spring配置上传文件大小
上传文件过大时,不会进入控制层,会直接抛出异常,提示上传文件过大,如下: org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededE ...
- django -- verbose_name的对数据库层面的影响
一.没有verbose_name时model的定义: from django.db import models # Create your models here. class Question(mo ...
- 关于ansbile工具的shell、command、script、raw模块的区别和使用场景
command模块 [执行远程命令] [root@node1 ansible]# ansible testservers -m command -a 'uname -n' script模块 [在远程主 ...
- JDK1.6新特性,网络增强(Networking features and enhancements)
参考: http://docs.oracle.com/javase/6/docs/technotes/guides/net/enhancements-6.0.html http://blog.csdn ...
- 解决sweetalert 无故报错 elem.className.replace Uncaught TypeError: Cannot read property 'className' of null
今天碰到这么一个问题,在使用sweetalert的时候时有时无会报错 elem.className.replace Uncaught TypeError: Cannot read property ' ...
- 我用Xamarin开发android应用,应用在真机上一打开就退出了
在解决方案管理器的项目上右键--属性--Android Options--Packaging将Use Shared Runtime前面的对勾取消即可.
- iOS 基础-----关于UIView 的 frame 与 bounds
首先,对于frame 大家都很熟悉,是当前view ,相对于其父视图view 的坐标,例如: UIView *view1 = [[UIView alloc] initWithFrame:CGRectM ...
- 利用Angular.js从PHP读取后台数据
之前已经有非常多方法能够通过angular进行本地数据的读取.曾经的样例中,大多数情况都是将数据存放到模块的$scope变量中,或者直接利用ng-init定义初始化的数据. 可是这些方法都仅仅为了演示 ...