postgresql with递归

本文介绍了PostgreSQL中with子句的应用,通过实例演示了如何使用with子句简化复杂查询,包括基本用法、多个with子句的组合及递归查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1. 建表
[sql]  view plain  copy
 
  1. postgres=# create table tb9(id serial primary key,name character varying, parentid integer);  
  2. CREATE TABLE  
[sql]  view plain  copy
 
  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. 插入测试数据
[sql]  view plain  copy
 
  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  
[sql]  view plain  copy
 
  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子句
[sql]  view plain  copy
 
  1. postgres=# with t as (select * from tb9 where parentid=1) select count(0) from t;  
  2.  count   
  3. -------  
  4.      2  
  5. (1 row)  
[sql]  view plain  copy
 
  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的记录的所有子记录
[sql]  view plain  copy
 
  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的记录的所有子记录
[sql]  view plain  copy
 
  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.youkuaiyun.com/luojinbai/article/details/44015581
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值