1. 程式人生 > >WITH RECURSIVE遞迴(4個例子)

WITH RECURSIVE遞迴(4個例子)

計算1到100的累加的結果。
WITH RECURSIVE t(n) AS (
    VALUES (1)
  UNION ALL
    SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t;
sum

5050

============================================================================

/*
https://www.yiibai.com/html/postgresql/2013/080565.html
create table company (
	id int ,
	name varchar (60),
	age int ,
	address varchar (30),
	salary int
);
insert into company values(1,'Paul',32,'California',20000);
insert into company values(2,'Allen',25, 'Texas',15000);
insert into company values(3,'Teddy',23,'Norway',20000);
insert into company values(4,'Mark',25,'Rich-Mond',65000);
insert into company values(5,'David',27,'Texas',85000);
insert into company values(6,'Kim',22,'South-Hall',45000);
insert into company values(7,'James',24,'Houston',10000);

*/

--遞迴WITH
--找到小於20000的薪金總和

WITH RECURSIVE t(n) AS (
		VALUES (0)
		UNION ALL
		SELECT salary FROM company WHERE salary < 20000
)
SELECT SUM(n) FROM t

--刪除指定的公司行,RETURNING子句返回它們的內容;然後主查詢讀取輸出,並將其插入到COMPANY1 表:

CREATE TABLE COMPANY1(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);
WITH moved_rows AS (
    DELETE FROM COMPANY
    WHERE
        SALARY >= 30000
    RETURNING *
)
INSERT INTO COMPANY1 (SELECT * FROM moved_rows);

SELECT * FROM COMPANY;
SELECT * FROM COMPANY1;



============================================================================

PS的HELP也沒有講得非常仔細,個人理解 :
1、
 SELECT * FROM emp WHERE empid = 2 執行後,存入1個臨時表
2、
執行 SELECT emp.* FROM emp, r WHERE emp.empparid = r.empid ,
結果為
 5 | 2 | MANAGER
3、繼續執行  SELECT emp.* FROM emp, r WHERE emp.empparid = r.empid 直到
記錄集為空(沒有滿足條件的記錄);
4、將1、2、3的結果UNION ALL後輸出。

============================================================================

定義下面這樣的表,儲存每個區域(省、市、區)的id,名字及上級區域的pid

create table tb(id varchar(3) , pid varchar(3) , name varchar(10)); 

insert into tb values('002' , 0 , '浙江省'); 
insert into tb values('001' , 0 , '廣東省'); 
insert into tb values('003' , '002' , '衢州市');  
insert into tb values('004' , '002' , '杭州市') ; 
insert into tb values('005' , '002' , '湖州市');  
insert into tb values('006' , '002' , '嘉興市') ; 
insert into tb values('007' , '002' , '寧波市');  
insert into tb values('008' , '002' , '紹興市') ; 
insert into tb values('009' , '002' , '台州市');  
insert into tb values('010' , '002' , '溫州市') ; 
insert into tb values('011' , '002' , '麗水市');  
insert into tb values('012' , '002' , '金華市') ; 
insert into tb values('013' , '002' , '舟山市');  
insert into tb values('014' , '004' , '上城區') ; 
insert into tb values('015' , '004' , '下城區');  
insert into tb values('016' , '004' , '拱墅區') ; 
insert into tb values('017' , '004' , '餘杭區') ; 
insert into tb values('018' , '011' , '金東區') ; 
insert into tb values('019' , '001' , '廣州市') ; 
insert into tb values('020' , '001' , '深圳市') ;
需要查出某個省,比如浙江省,管轄的所有市及市轄地區
with RECURSIVE cte as
(
select a.id,cast(a.name as varchar(100)) from tb a where id='002'
union all 
select k.id,cast(c.name||'>'||k.name as varchar(100)) as name  from tb k inner join cte c on c.id = k.pid
)select id,name from cte ;