pgsql中層次查詢方法

2022-04-30 17:15:13 字數 1494 閱讀 2946

與oracle中的start with ... connect by..非常相似的功能,查詢後顯示目錄層次結構的方法:

當然在sql server 2005之後有with的功能。

測試**:

drop

table

ifexists t1;

create

table t1(id integer

primary

key,

name varchar(32),

parent integer);

insert

into t1 values(1, '

中國', 0);

insert

into t1 values(2, '

江蘇', 1);

insert

into t1 values(3, '

浙江', 1);

insert

into t1 values(4, '

上海', 1);

insert

into t1 values(5, '

南京', 2);

insert

into t1 values(6, '

蘇州', 2);

insert

into t1 values(7, '

杭州', 3);

insert

into t1 values(8, '

寧波', 3);

insert

into t1 values(9, '

崑山', 6);

insert

into t1 values(10,'

常熟', 6);

insert

into t1 values(11,'

吳江', 6);

實現**:

with recursive x as(select t1.*, t1.id::text

as path

from t1

where layer =

1union

allselect t1.*, path ||'|

'|| t1.id

from t1 join x

on t1.parent = x.id)

select

*from x order

by path;

with recursive r as ( 

select

*from t1 where id =

6union

allselect t1.*

from t1, r where t1.parent = r.id

) select

*from r order

by id;

pgsql處理文件型別資料 pgsql使用文件

pgsql使用文件 也是一種資料庫。安裝yum install postgresql server y 基礎配置 使用mkdir建立資料目錄 mkdir data postgres 使用chown賦予postgres data postgres許可權 chown postgres.postgres ...

談談 中層管理

目前發現工作中上級領導的風格千秋各異,有的會讓人覺得很nice,有的會讓人覺得很難受,這裡說說我對中層管理的個人看法吧 不久前,我一同事都快轉正了,都已經約好答辯時間了,忽然選擇了辭職。這事我覺得挺可惜的,我們坐的較近,就聊了一下,才知道事情原委,原來是因為之前幾次上級的做事方式和他鬧了幾次不愉快,...

pgSQL 集群過程

需求 工作需要 3種集群模式 pgcluster.全熱備集群.都讀寫.每台節點都保持資料完整 pg slony i or ii.主從集群.從唯讀.主讀寫 pg plporxy.分流特性.負載平衡.分布到節點上 目的 在 plproxy 上做查詢,從node返回結果.環境 3臺centos 1臺做pl...