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');