1. 程式人生 > >PostgreSQL的遞迴查詢

PostgreSQL的遞迴查詢

表結構 如下 地區表
CREATE TABLE "public"."region" (
"region_id" int4 NOT NULL,
"parent_id" int4,
"region_name" varchar(50) COLLATE "default" NOT NULL,
"longitude" varchar(10) COLLATE "default",
"latitude" varchar(10) COLLATE "default",
"region_code" varchar(10) COLLATE "default",
CONSTRAINT "pk_p_region2" PRIMARY KEY ("region_id"),
CONSTRAINT "fk_p_region_reference_region2" FOREIGN KEY ("parent_id") REFERENCES "public"."region" ("region_id") ON DELETE RESTRICT ON UPDATE RESTRICT
)
WITH (OIDS=FALSE)
;

父類查詢子類

WITH RECURSIVE r AS (  
  
       SELECT * FROM Region WHERE region_id =8653 
  
     union   ALL  
  
       SELECT Region.* FROM Region, r WHERE Region.PARENT_ID = r.region_id  
  
     )    
SELECT * FROM r ORDER BY region_id; 

子類遞迴查詢所有父類

WITH RECURSIVE r AS (  
  
       SELECT * FROM Region WHERE region_id =8675
  
     union   ALL  
  
       SELECT Region.* FROM Region, r WHERE  Region.region_id= r.PARENT_ID  
  
     )    
SELECT * FROM r ORDER BY region_id; 

部分資料 

INSERT INTO "public"."region" VALUES ('100', '8653', '廣州市', null, null, '4401');

INSERT INTO "public"."region" VALUES ('6711', null, '北京市', null, null, '11');
INSERT INTO "public"."region" VALUES ('6712', '6711', '東城區', null, null, '110101');
INSERT INTO "public"."region" VALUES ('6713', '6711', '西城區', null, null, '110102');
INSERT INTO "public"."region" VALUES ('6714', '6711', '朝陽區', null, null, '110105');
INSERT INTO "public"."region" VALUES ('6715', '6711', '豐臺區', null, null, '110106');
INSERT INTO "public"."region" VALUES ('6716', '6711', '石景山區', null, null, '110107');
INSERT INTO "public"."region" VALUES ('6717', '6711', '海淀區', null, null, '110108');
INSERT INTO "public"."region" VALUES ('6718', '6711', '門頭溝區', null, null, '110109');
INSERT INTO "public"."region" VALUES ('6719', '6711', '房山區', null, null, '110111');
INSERT INTO "public"."region" VALUES ('6720', '6711', '通州區', null, null, '110112');
INSERT INTO "public"."region" VALUES ('6721', '6711', '順義區', null, null, '110113');
INSERT INTO "public"."region" VALUES ('6722', '6711', '昌平區', null, null, '110114');
INSERT INTO "public"."region" VALUES ('6723', '6711', '大興區', null, null, '110115');
INSERT INTO "public"."region" VALUES ('6724', '6711', '懷柔區', null, null, '110116');
INSERT INTO "public"."region" VALUES ('6725', '6711', '平谷區', null, null, '110117');
INSERT INTO "public"."region" VALUES ('6726', '6711', '密雲縣', null, null, '110228');
INSERT INTO "public"."region" VALUES ('6727', '6711', '延慶縣', null, null, '110229');
INSERT INTO "public"."region" VALUES ('6728', null, '天津市', null, null, '12');
INSERT INTO "public"."region" VALUES ('6729', '6728', '和平區', null, null, '120101');
INSERT INTO "public"."region" VALUES ('6730', '6728', '河東區', null, null, '120102');
INSERT INTO "public"."region" VALUES ('6731', '6728', '河西區', null, null, '120103');
INSERT INTO "public"."region" VALUES ('6732', '6728', '南開區', null, null, '120104');
INSERT INTO "public"."region" VALUES ('6733', '6728', '河北區', null, null, '120105');
INSERT INTO "public"."region" VALUES ('6734', '6728', '紅橋區', null, null, '120106');
INSERT INTO "public"."region" VALUES ('6735', '6728', '東麗區', null, null, '120110');
INSERT INTO "public"."region" VALUES ('6736', '6728', '西青區', null, null, '120111');
INSERT INTO "public"."region" VALUES ('6737', '6728', '津南區', null, null, '120112');
INSERT INTO "public"."region" VALUES ('6738', '6728', '北辰區', null, null, '120113');
INSERT INTO "public"."region" VALUES ('6739', '6728', '武清區', null, null, '120114');
INSERT INTO "public"."region" VALUES ('6740', '6728', '寶坻區', null, null, '120115');
INSERT INTO "public"."region" VALUES ('6741', '6728', '濱海新區', null, null, '120116');
INSERT INTO "public"."region" VALUES ('6742', '6728', '寧河縣', null, null, '120221');
INSERT INTO "public"."region" VALUES ('6743', '6728', '靜海縣', null, null, '120223');
INSERT INTO "public"."region" VALUES ('6744', '6728', '薊縣', null, null, '120225');
INSERT INTO "public"."region" VALUES ('6745', null, '河北省', null, null, '13');
INSERT INTO "public"."region" VALUES ('6746', '6745', '石家莊市', null, null, '1301');
INSERT INTO "public"."region" VALUES ('6747', '6746', '長安區', null, null, '130102');
INSERT INTO "public"."region" VALUES ('6748', '6746', '橋西區', null, null, '130104');
INSERT INTO "public"."region" VALUES ('6749', '6746', '新華區', null, null, '130105');
INSERT INTO "public"."region" VALUES ('6750', '6746', '井陘礦區', null, null, '130107');
INSERT INTO "public"."region" VALUES ('6751', '6746', '裕華區', null, null, '130108');
INSERT INTO "public"."region" VALUES ('6752', '6746', '藁城區', null, null, '130109');
INSERT INTO "public"."region" VALUES ('6753', '6746', '鹿泉區', null, null, '130110');
INSERT INTO "public"."region" VALUES ('6754', '6746', '欒城區', null, null, '130111');
INSERT INTO "public"."region" VALUES ('6755', '6746', '井陘縣', null, null, '130121');
INSERT INTO "public"."region" VALUES ('6756', '6746', '正定縣', null, null, '130123');
INSERT INTO "public"."region" VALUES ('6757', '6746', '行唐縣', null, null, '130125');
INSERT INTO "public"."region" VALUES ('6758', '6746', '靈壽縣', null, null, '130126');
INSERT INTO "public"."region" VALUES ('6759', '6746', '高邑縣', null, null, '130127');
INSERT INTO "public"."region" VALUES ('6760', '6746', '深澤縣', null, null, '130128');
INSERT INTO "public"."region" VALUES ('6761', '6746', '贊皇縣', null, null, '130129');
INSERT INTO "public"."region" VALUES ('6762', '6746', '無極縣', null, null, '130130');
INSERT INTO "public"."region" VALUES ('6763', '6746', '平山縣', null, null, '130131');
INSERT INTO "public"."region" VALUES ('6764', '6746', '元氏縣', null, null, '130132');
INSERT INTO "public"."region" VALUES ('6765', '6746', '趙縣', null, null, '130133');
INSERT INTO "public"."region" VALUES ('6766', '6746', '辛集市', null, null, '130181');
INSERT INTO "public"."region" VALUES ('6767', '6746', '晉州市', null, null, '130183');
INSERT INTO "public"."region" VALUES ('6768', '6746', '新樂市', null, null, '130184');
INSERT INTO "public"."region" VALUES ('6769', '6745', '唐山市', null, null, '1302');
INSERT INTO "public"."region" VALUES ('6770', '6769', '路南區', null, null, '130202');
INSERT INTO "public"."region" VALUES ('6771', '6769', '路北區', null, null, '130203');
INSERT INTO "public"."region" VALUES ('6772', '6769', '古冶區', null, null, '130204');
INSERT INTO "public"."region" VALUES ('6773', '6769', '開平區', null, null, '130205');
INSERT INTO "public"."region" VALUES ('6774', '6769', '豐南區', null, null, '130207');
INSERT INTO "public"."region" VALUES ('6775', '6769', '豐潤區', null, null, '130208');
INSERT INTO "public"."region" VALUES ('6776', '6769', '曹妃甸區', null, null, '130209');
INSERT INTO "public"."region" VALUES ('6777', '6769', '灤縣', null, null, '130223');
INSERT INTO "public"."region" VALUES ('6778', '6769', '灤南縣', null, null, '130224');
INSERT INTO "public"."region" VALUES ('6779', '6769', '樂亭縣', null, null, '130225');
INSERT INTO "public"."region" VALUES ('6780', '6769', '遷西縣', null, null, '130227');
INSERT INTO "public"."region" VALUES ('6781', '6769', '玉田縣', null, null, '130229');
INSERT INTO "public"."region" VALUES ('6782', '6769', '遵化市', null, null, '130281');
INSERT INTO "public"."region" VALUES ('6783', '6769', '遷安市', null, null, '130283');
INSERT INTO "public"."region" VALUES ('6784', '6745', '秦皇島市', null, null, '1303');
INSERT INTO "public"."region" VALUES ('6785', '6784', '海港區', null, null, '130302');
INSERT INTO "public"."region" VALUES ('6786', '6784', '山海關區', null, null, '130303');
INSERT INTO "public"."region" VALUES ('6787', '6784', '北戴河區', null, null, '130304');
INSERT INTO "public"."region" VALUES ('6788', '6784', '青龍滿族自治縣', null, null, '130321');
INSERT INTO "public"."region" VALUES ('6789', '6784', '昌黎縣', null, null, '130322');
INSERT INTO "public"."region" VALUES ('6790', '6784', '撫寧縣', null, null, '130323');
INSERT INTO "public"."region" VALUES ('6791', '6784', '盧龍縣', null, null, '130324');
INSERT INTO "public"."region" VALUES ('6792', '6745', '邯鄲市', null, null, '1304');
INSERT INTO "public"."region" VALUES ('6793', '6792', '邯山區', null, null, '130402');
INSERT INTO "public"."region" VALUES ('6794', '6792', '叢臺區', null, null, '130403');
INSERT INTO "public"."region" VALUES ('6795', '6792', '復興區', null, null, '130404');
INSERT INTO "public"."region" VALUES ('6796', '6792', '峰峰礦區', null, null, '130406');
INSERT INTO "public"."region" VALUES ('6797', '6792', '邯鄲縣', null, null, '130421');
INSERT INTO "public"."region" VALUES ('6798', '6792', '臨漳縣', null, null, '130423');
INSERT INTO "public"."region" VALUES ('6799', '6792', '成安縣', null, null, '130424');
INSERT INTO "public"."region" VALUES ('6800', '6792', '大名縣', null, null, '130425');
INSERT INTO "public"."region" VALUES ('6801', '6792', '涉縣', null, null, '130426');
INSERT INTO "public"."region" VALUES ('6802', '6792', '磁縣', null, null, '130427');
INSERT INTO "public"."region" VALUES ('6803', '6792', '肥鄉縣', null, null, '130428');
INSERT INTO "public"."region" VALUES ('6804', '6792', '永年縣', null, null, '130429');
INSERT INTO "public"."region" VALUES ('6805', '6792', '邱縣', null, null, '130430');
INSERT INTO "public"."region" VALUES ('6806', '6792', '雞澤縣', null, null, '130431');
INSERT INTO "public"."region" VALUES ('6807', '6792', '廣平縣', null, null, '130432');
INSERT INTO "public"."region" VALUES ('6808', '6792', '館陶縣', null, null, '130433');
INSERT INTO "public"."region" VALUES ('6809', '6792', '魏縣', null, null, '130434');
INSERT INTO "public"."region" VALUES ('6810', '6792', '曲周縣', null, null, '130435');
INSERT INTO "public"."region" VALUES ('6811', '6792', '武安市', null, null, '130481');
INSERT INTO "public"."region" VALUES ('6812', '6745', '邢臺市', null, null, '1305');
INSERT INTO "public"."region" VALUES ('6813', '6812', '橋東區', null, null, '130502');
INSERT INTO "public"."region" VALUES ('6814', '6812', '橋西區', null, null, '130503');
INSERT INTO "public"."region" VALUES ('6815', '6812', '邢臺縣', null, null, '130521');
INSERT INTO "public"."region" VALUES ('6816', '6812', '臨城縣', null, null, '130522');

相關推薦

postgresql查詢並將結果拼接

pg由於對大小寫不敏感,如果欄位中有大寫,需要將該欄位加雙引號,否則自動轉為小寫查詢with RECURSIVE cte as  (  select a.id,a.name,a."parentId" from system_area a where id=130000  un

Postgresql查詢

資料表 with RECURSIVE cte as   (   select a.* from department a where name='東南片區'    union all     select k.*  from department k inner joi

PostgreSQL查詢實現樹狀結構查詢

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

PostgreSQL查詢

表結構 如下 地區表CREATE TABLE "public"."region" ( "region_id" int4 NOT NULL, "parent_id" int4, "region_name" varchar(50) COLLATE "default" NOT NU

postgresql可達性問題 查詢

FLIGHTSOrigin:Destination:ABACBCCDCREATE TABLE flights(origin varchar(5),destination varchar(5));INSERT INTO flights values('A','B'),('A',

postgresql with 查詢

Oracle資料庫中的用 CONNECT BY來做 遞迴 查 詢。  PostgreSQL8.3以前是用connectby()函式來做遞迴 查 詢 。 connectby() 函式是 contrib/tablefunc模 塊 下的功能,默 認 是沒有安裝的,需要自己安裝。 

postgresql的分頁顯示-擷取字串-查詢

======================================= >>>>>>>>>>>>>>postgresql<<<<<<<<

mybatis 實現查詢出樹結構節點

mybatis 實現遞迴查詢出樹結構節點 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.

MySQL自定義函式查詢

用於遞迴查詢Id(通過parentId關聯)引數為int 型別的值: CREATE DEFINER=`root`@`%` FUNCTION `getChildList`(rootId INT) RETURNS text CHARSET utf8 BEGIN DECLARE sTemp

sql查詢子類

平時工作中我們會遇到主從層次關係的結構資料,我們需要把資料取出來並且提現出層級就像樹形結構一樣,比如這樣的結構: 資料庫表結構如下,有個parent_id和sub_id,就是把兩者的關係儲存起來。 id為768的下面有769,770,771,772,780,781資料,同時

java tree 結構查詢

create table TB_TREE ( CID NUMBER not null, CNAME VARCHAR2(50), PID NUMBER //父節點 ) insert into tb_tree (CID, CNAME, PID) values (1, '中國', 0);

mysql查詢,mysql中從子類ID查詢所有父類(做無限分類經常用到)

由於mysql 不支援類似 oracle with ...connect的 遞迴查詢語法之前一直以為類似的查詢要麼用儲存過程要麼只能用程式寫遞迴查詢.現在發現原來一條sql語句也是可以搞定的先來看資料表的結構如下:id  name    parent_id&n

mysql8.0CTE實現查詢

+----+----------+--------------+| ID | ParentID | name         |+----+----------+--------------+|  1 | &n

Oracle 查詢:start with

 什麼時候用到start with ?    (1) 一張表中存放有目錄樹的相關資料(子類id , 父類id )    (2)但是想展示為父子型別的資料給前臺,或者列表(table)中,    (3) 這個時候就可以考

Oracle樹查詢,start with connect by prior 查詢用法(轉載)

本人覺得這個寫的真不錯,實用性強,就轉載過來了 這個子句主要是用於B樹結構型別的資料遞迴查詢,給出B樹結構型別中的任意一個結點,遍歷其最終父結點或者子結點。 先看原始資料: 1 create table a_test 2 ( parentid varchar2(10), 3

Oracle 樹操作、查詢(select…start with…connect by…prior)

一、Oracle中start with…connect by prior子句用法 connect by 是結構化查詢中用到的,其基本語法是: select … from tablename start with 條件1connect by 條件2where 條件3; 例: select

DNS查詢與迭代查詢

DNS遞迴查詢與迭代查詢 summary 一直以來對於DNS查詢的“遞迴”與“迭代”方式感到困惑。一般人就直接跟你說“DNS客戶端向DNS伺服器請求叫遞迴查詢”,“DNS伺服器之間的查詢請求是迭代查詢”,聽了之後根本不知所謂。。。直到我看了《網路作業系統——windows se

oracle查詢報錯

SELECT * FROM T_OrgCom t where t.systemcode = '04' and t.ENABLED = '1' START WITH t.id='00000000' CONNECT BY   PRIOR t.ID=t.pid 報錯:使用者資料中的co

Unity學習筆記003.查詢子物體/獲取子物體元件

public static Transform FindChild(Transform parent,string name) { Transform child = null; child = parent.Find(name); if (child != null)

用mysql儲存過程代替查詢 用mysql儲存過程代替查詢

用mysql儲存過程代替遞迴查詢 查詢此表某個id=4028ab535e370cd7015e37835f52014b(公司1)下的所有資料 正常情況下,我們採用遞迴演算法查詢,如下 1