在PostgreSQL里,with子句提供了一种方法写一个大的查询中使用的辅助报表与查询。它有助于打破复杂和大型查询简单易读的形式。

1. 建表

  1. postgres=# create table tb9(id serial primary key,name character varying, parentid integer);
  2. CREATE TABLE
  1. postgres=# \d tb9
  2. Table "public.tb9"
  3. Column  |       Type        |                    Modifiers
  4. ----------+-------------------+--------------------------------------------------
  5. id       | integer           | not null default nextval('tb9_id_seq'::regclass)
  6. name     | character varying |
  7. parentid | integer           |
  8. Indexes:
  9. "tb9_pkey" PRIMARY KEY, btree (id)

2. 插入测试数据

  1. postgres=# insert into tb9 values(generate_series(1,5),'john',0);
  2. INSERT 0 5
  3. postgres=# insert into tb9 values(6,'john1',1);
  4. INSERT 0 1
  5. postgres=# insert into tb9 values(7,'john2',1);
  6. INSERT 0 1
  7. postgres=# insert into tb9 values(8,'john11',6);
  8. INSERT 0 1
  1. postgres=# select * from tb9;
  2. id |  name  | parentid
  3. ----+--------+----------
  4. 1 | john   |        0
  5. 2 | john   |        0
  6. 3 | john   |        0
  7. 4 | john   |        0
  8. 5 | john   |        0
  9. 6 | john1  |        1
  10. 7 | john2  |        1
  11. 8 | john11 |        6
  12. (8 rows)

3. with子句

  1. postgres=# with t as (select * from tb9 where parentid=1) select count(0) from t;
  2. count
  3. -------
  4. 2
  5. (1 row)
  1. postgres=# with t(a,b,c) as (select * from tb9 where parentid=1) select a,b,c from t;
  2. a |   b   | c
  3. ---+-------+---
  4. 6 | john1 | 1
  5. 7 | john2 | 1
  6. (2 rows)

4. 多个with子句的结合使用
parentid=1的记录的所有子记录

  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;
  2. id |  name  | parentid
  3. ----+--------+----------
  4. 8 | john11 |        6
  5. (1 row)

5. 递归
id为1的记录的所有子记录

  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;
  2. id |  name  | parentid
  3. ----+--------+----------
  4. 1 | john   |        0
  5. 6 | john1  |        1
  6. 7 | john2  |        1
  7. 8 | john11 |        6
  8. 9 | john21 |        7
  9. (5 rows)
 
 转自 http://blog.csdn.net/luojinbai/article/details/44015581

postgresql with递归的更多相关文章

  1. MVCC PostgreSQL实现事务和多版本并发控制的精华

    原创文章,同步发自作者个人博客,http://www.jasongj.com/sql/mvcc/ PostgreSQL针对ACID的实现机制 事务的实现原理可以解读为RDBMS采取何种技术确保事务的A ...

  2. PostgreSQL中RECURSIVE递归查询使用总结

    RECURSIVE 前言 CTE or WITH 在WITH中使用数据修改语句 WITH使用注意事项 RECURSIVE 递归查询的过程 拆解下执行的过程 1.执行非递归部分 2.执行递归部分,如果是 ...

  3. 一道Postgresql递归树题

    转载请注明出处: https://www.cnblogs.com/funnyzpc/p/13698249.html 也是偶然的一次,群友出了一道题考考大家,当时正值疫情最最严重的三月(借口...),披 ...

  4. PostgreSQL 与 MySQL 相比,优势何在?

    一. PostgreSQL 的稳定性极强, Innodb 等引擎在崩溃.断电之类的灾难场景下抗打击能力有了长足进步,然而很多 MySQL 用户都遇到过Server级的数据库丢失的场景——mysql系统 ...

  5. postgresql查询的处理过程

    本文简单描述了Postgresql服务器接收到查询后到返回给客户端结果之间一般操作顺序,总的流程图如下: 第一步: 客户端程序可以是任何符合 PostgreSQL 协议规范的程序,如 JDBC 驱动. ...

  6. PostgreSQL笔记

    本文针对目前最新版9.5.1,若非说明,文中所说文档即指官方文档.本人刚接触PostgreSQL不久,文中不免错漏,请大家指正:随着了解深入,本文[可能]会不定期更新补足. JSON PostgreS ...

  7. 安装postgreSQL出现configure:error:readline library not found解决方法

    要安装 readline , readline-dev 开发包,要么使用 --without-readline 选项关闭 readline 功能. #yum install readline; #yu ...

  8. 跟我一起读postgresql源码(七)——Executor(查询执行模块之——数据定义语句的执行)

    1.数据定义语句的执行 数据定义语句(也就是之前我提到的非可优化语句)是一类用于定义数据模式.函数等的功能性语句.不同于元组增删査改的操作,其处理方式是为每一种类型的描述语句调用相应的处理函数. 数据 ...

  9. 跟我一起读postgresql源码(八)——Executor(查询执行模块之——可优化语句的执行)

    2.可优化语句的执行 可优化语句的共同特点是它们被查询编译器处理后都会生成査询计划树,这一类语句由执行器(Executor)处理.该模块对外提供了三个接口: ExecutorStart.Executo ...

随机推荐

  1. android发送短信代码(短信内容超长处理)

    一条短信只可容纳70个中文,所以当短信长度超过70个中文字符时程序就要特殊处理了. 有两种方式: 1.通过sendTextMessage()方法依次发送拆分后的短信,该方式有个弊端就是用户会分条收到短 ...

  2. Unity投影器细节整理

    抽了个空整理下投影器 一般投影器需要两张贴图,一张Cookie,一张FallOff. Unity提供Light和Multiple两种自带shader,和粒子类似. Cookie需要非alpha贴图,F ...

  3. 打造 Vue.js 可复用组件

    Vue.js 是一套构建用户界面的渐进式框架.我们可以使用简单的 API 来实现响应式的数据绑定和组合的视图组件. 从维护视图到维护数据,Vue.js 让我们快速地开发应用.但随着业务代码日益庞大,组 ...

  4. 根据第三方提供的webservice地址获取文件信息

    import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.en ...

  5. Flink 中的kafka何时commit?

    https://ci.apache.org/projects/flink/flink-docs-release-1.6/internals/stream_checkpointing.html @Ove ...

  6. debian8.5安装sublime text3

    在官网www.sublimetext.com下载安装包 我这里用的是Ubuntu 64 bit版. 下载后使用su命令切换到root账户. 执行安装命令 dpkg -i sublime-text*.d ...

  7. matlab: Attempt to execute SCRIPT *** as a function 错误

    编写matlab程序时,出现了“Attempt to execute SCRIPT mean as a function”,其实这是“Attempt to execute SCRIPT *** as ...

  8. 每日英语:China Bond Trading Dives

    SHANGHAI—Trading volume in China's bond market has plummeted in recent months, in another reminder o ...

  9. 每日英语:Chinese Show Global Real-Estate Appetite

    Chinese investors have been snapping up real estate in the world's most important cities this year. ...

  10. make -C M=

    http://blog.sina.com.cn/s/blog_89fa41ef0100trjr.html Makefile:PWD = $(shell pwd)KERNEL_SRC = /usr/sr ...