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 ...
随机推荐
- PowerShell 获取Site Collection下被签出的文件
由于权限的设置,当文件被签出时导致别人不可见了,这对校验文件个数的人来说着实是件烦恼的事.幸好利用PowerShell,可以获取Site Collection下被签出的文件. Resolution A ...
- Java – Stream has already been operated upon or closed
Java – Stream has already been operated upon or closed package com.mkyong.java8; import java.util.Ar ...
- 温故而知新 Ajax 的新坑 dataType: 'json'
为了方便实验,我随便捏造了一个json数据,然后放在php中输出. 请求明明是200,json数据也正确,但ajax就是不执行success回调? 原因是 dataType: 'json', 导致的. ...
- springboot 与 mybatis 中事务特性讲解
1 MyBatis自动参与到 spring 事务管理中,无需额外配置,只要org.mybatis.spring.SqlSessionFactoryBean引用的数据源与 DataSourceTrans ...
- MySQL 自带工具使用介绍
MySQL 数据库不仅提供了数据库的服务器端应用程序,同时还提供了大量的客户端工具程序,如mysql,mysqladmin,mysqldump 等等,都是大家所熟悉的.虽然有些人对这些工具的功能都已经 ...
- 《深入理解jvm》笔记---第六章
类文件结构 1. Java一次编写,到处执行的基石: Java编译产生的是字节码(bytecode).sun公司和其它虚拟机提供商公布各个平台上的虚拟机.这些虚拟机能够加载和执行这些与平台无关的 ...
- HTML框架标签的使用-<frameset>
<html> <head> <title> frameset框架的使用-使用frameset框架进行布局 </title> <!-- 标签名称:f ...
- 启动 angular-phonecat 项目时出现这玩意 。('The header content contains invalid characters');
最近学习angular, 跟着视频做一个动作,启动 “ angular-phonecat ” 这个项目 敲入 “npm start ” 启动没有问题,但是 "http://localhost ...
- vivado2015.4 simulator 存储所有信号到 .wdb 文件 并打开波形文件查看波形
OS WIN7vivado 2015.4vivado自带的仿真器 vivado project 包含一个block design, block design 中包含AXIPCIE, MIG, INTE ...
- python 文件目录遍历
递归遍历目录和文件 import os path = r'F:\PycharmProjects\basic gram\作业和习题\test' def getAllFileAndDir(path): # ...