postgreSQL 遞迴查詢,使用陣列

2021-10-19 05:09:05 字數 3074 閱讀 4234

遞迴查詢使用with recursive

陣列使用array

運用場景:用於統計多個層級中某乙個層級以及下面所有分類的資料

表結構:

drop table if exists "public"."test_tree";

create table "public"."test_tree" (

"id" int4 not null,

"code" varchar(50) collate "pg_catalog"."default",

"name" varchar(128) collate "pg_catalog"."default",

"pid" int4);

comment on column "public"."test_tree"."code" is '編碼';

comment on column "public"."test_tree"."name" is '名稱';

comment on column "public"."test_tree"."pid" is '上級id';

comment on table "public"."test_tree" is '測試表';

-- ----------------------------

-- records of test_tree

-- ----------------------------

insert into "public"."test_tree" values (1, '1', '動物', 0);

insert into "public"."test_tree" values (2, '02000', '貓', 1);

insert into "public"."test_tree" values (3, '03000', '狗', 1);

insert into "public"."test_tree" values (4, '01000', '豬', 1);

insert into "public"."test_tree" values (10, '02001', '英短', 2);

insert into "public"."test_tree" values (11, '02002', '美短', 2);

insert into "public"."test_tree" values (12, '02003', '橘貓', 2);

insert into "public"."test_tree" values (13, '02004', '布偶', 2);

insert into "public"."test_tree" values (14, '02005', '三花', 2);

insert into "public"."test_tree" values (15, '03001', '薩摩', 3);

insert into "public"."test_tree" values (16, '03002', '藏獒', 3);

insert into "public"."test_tree" values (17, '03003', '博美', 3);

insert into "public"."test_tree" values (18, '03004', '泰迪', 3);

insert into "public"."test_tree" values (19, '03005', '二哈', 3);

insert into "public"."test_tree" values (5, '01001', '紅豬', 4);

insert into "public"."test_tree" values (6, '01002', '綠豬', 4);

insert into "public"."test_tree" values (7, '01003', '藍豬', 4);

insert into "public"."test_tree" values (8, '01004', '黃豬', 4);

insert into "public"."test_tree" values (9, '01005', '紫豬', 4);

-- ----------------------------

-- primary key structure for table test_tree

-- ----------------------------

alter table "public"."test_tree" add constraint "test_tree_pkey1" primary key ("id");

查詢sql:(本例子設定了3個層級,實際運用中層級不做限制,不受影響)

with recursive le ( id, code, name, pid, path, level ) as (

select

id,code,

name,

pid,

array [ id ] as path,

1 as level

from

test_tree

where

pid in ( 0 ) union all

select

e2.id,

e2.code,

e2.name,

e2.pid,

e3.path || e3.id,

e3.level + 1

from

test_tree e2,

le e3

where

e3.id= e2.pid

) select

* from

le order by

rpad( level :: varchar, 5, '0' ) asc

結果如下:

path列是返回的級層陣列,通過path[1]、path[2]等方式可以取出對應值

PostgreSQL 遞迴查詢應用場景

今天在罈子裡有人提出了乙個問題,問題是這樣的 在以下指定表中 id name fatherid 1 中國 0 2 遼寧 1 3 山東 1 4 瀋陽 2 5 大連 2 6 濟南 3 7 和平區 4 8 沈河區 4 現在給定乙個id號,想得到它完整的名字。如 當id 7時,名字是 中國遼寧瀋陽和平區 當...

PostgreSQL遞迴查詢實現樹狀結構查詢

在postgresql的使用過程中發現了乙個很有意思的功能,就是對於需要類似於樹狀結構的結果可以使用遞迴查詢實現。比如說我們常用的公司部門這種資料結構,一般我們設計表結構的時候都是類似下面的sql,其中parent id為null時表示頂級節點,否則表示上級節點id。create table dep...

PostgreSQL遞迴查詢(從子到父 從父到子)

結語場景 資料庫中的資料存在父子關係 單繼承,每一條記錄只有乙個父記錄 如果要查詢一條記錄以及他的所有子記錄,或者要查詢一條記錄以及他的所有父記錄.那麼遞迴查詢就再合適不過了.可以簡化複雜的sql語句。現在資料庫有一張dictionary表,用於存放業務相關字典項 id,name,parent id...