PostgreSQL的递归查询

PostgreSQL的递归查询,第1张

概述表结构 如下 地区表 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" var 表结构 如下 地区表
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,'4401');

INSERT INTO "public"."region" VALUES ('6711','北京市','11'); INSERT INTO "public"."region" VALUES ('6712','6711','东城区','110101'); INSERT INTO "public"."region" VALUES ('6713','西城区','110102'); INSERT INTO "public"."region" VALUES ('6714','朝阳区','110105'); INSERT INTO "public"."region" VALUES ('6715','丰台区','110106'); INSERT INTO "public"."region" VALUES ('6716','石景山区','110107'); INSERT INTO "public"."region" VALUES ('6717','海淀区','110108'); INSERT INTO "public"."region" VALUES ('6718','门头沟区','110109'); INSERT INTO "public"."region" VALUES ('6719','房山区','110111'); INSERT INTO "public"."region" VALUES ('6720','通州区','110112'); INSERT INTO "public"."region" VALUES ('6721','顺义区','110113'); INSERT INTO "public"."region" VALUES ('6722','昌平区','110114'); INSERT INTO "public"."region" VALUES ('6723','大兴区','110115'); INSERT INTO "public"."region" VALUES ('6724','怀柔区','110116'); INSERT INTO "public"."region" VALUES ('6725','平谷区','110117'); INSERT INTO "public"."region" VALUES ('6726','密云县','110228'); INSERT INTO "public"."region" VALUES ('6727','延庆县','110229'); INSERT INTO "public"."region" VALUES ('6728','天津市','12'); INSERT INTO "public"."region" VALUES ('6729','6728','和平区','120101'); INSERT INTO "public"."region" VALUES ('6730','河东区','120102'); INSERT INTO "public"."region" VALUES ('6731','河西区','120103'); INSERT INTO "public"."region" VALUES ('6732','南开区','120104'); INSERT INTO "public"."region" VALUES ('6733','河北区','120105'); INSERT INTO "public"."region" VALUES ('6734','红桥区','120106'); INSERT INTO "public"."region" VALUES ('6735','东丽区','120110'); INSERT INTO "public"."region" VALUES ('6736','西青区','120111'); INSERT INTO "public"."region" VALUES ('6737','津南区','120112'); INSERT INTO "public"."region" VALUES ('6738','北辰区','120113'); INSERT INTO "public"."region" VALUES ('6739','武清区','120114'); INSERT INTO "public"."region" VALUES ('6740','宝坻区','120115'); INSERT INTO "public"."region" VALUES ('6741','滨海新区','120116'); INSERT INTO "public"."region" VALUES ('6742','宁河县','120221'); INSERT INTO "public"."region" VALUES ('6743','静海县','120223'); INSERT INTO "public"."region" VALUES ('6744','蓟县','120225'); INSERT INTO "public"."region" VALUES ('6745','河北省','13'); INSERT INTO "public"."region" VALUES ('6746','6745','石家庄市','1301'); INSERT INTO "public"."region" VALUES ('6747','6746','长安区','130102'); INSERT INTO "public"."region" VALUES ('6748','桥西区','130104'); INSERT INTO "public"."region" VALUES ('6749','新华区','130105'); INSERT INTO "public"."region" VALUES ('6750','井陉矿区','130107'); INSERT INTO "public"."region" VALUES ('6751','裕华区','130108'); INSERT INTO "public"."region" VALUES ('6752','藁城区','130109'); INSERT INTO "public"."region" VALUES ('6753','鹿泉区','130110'); INSERT INTO "public"."region" VALUES ('6754','栾城区','130111'); INSERT INTO "public"."region" VALUES ('6755','井陉县','130121'); INSERT INTO "public"."region" VALUES ('6756','正定县','130123'); INSERT INTO "public"."region" VALUES ('6757','行唐县','130125'); INSERT INTO "public"."region" VALUES ('6758','灵寿县','130126'); INSERT INTO "public"."region" VALUES ('6759','高邑县','130127'); INSERT INTO "public"."region" VALUES ('6760','深泽县','130128'); INSERT INTO "public"."region" VALUES ('6761','赞皇县','130129'); INSERT INTO "public"."region" VALUES ('6762','无极县','130130'); INSERT INTO "public"."region" VALUES ('6763','平山县','130131'); INSERT INTO "public"."region" VALUES ('6764','元氏县','130132'); INSERT INTO "public"."region" VALUES ('6765','赵县','130133'); INSERT INTO "public"."region" VALUES ('6766','辛集市','130181'); INSERT INTO "public"."region" VALUES ('6767','晋州市','130183'); INSERT INTO "public"."region" VALUES ('6768','新乐市','130184'); INSERT INTO "public"."region" VALUES ('6769','唐山市','1302'); INSERT INTO "public"."region" VALUES ('6770','6769','路南区','130202'); INSERT INTO "public"."region" VALUES ('6771','路北区','130203'); INSERT INTO "public"."region" VALUES ('6772','古冶区','130204'); INSERT INTO "public"."region" VALUES ('6773','开平区','130205'); INSERT INTO "public"."region" VALUES ('6774','丰南区','130207'); INSERT INTO "public"."region" VALUES ('6775','丰润区','130208'); INSERT INTO "public"."region" VALUES ('6776','曹妃甸区','130209'); INSERT INTO "public"."region" VALUES ('6777','滦县','130223'); INSERT INTO "public"."region" VALUES ('6778','滦南县','130224'); INSERT INTO "public"."region" VALUES ('6779','乐亭县','130225'); INSERT INTO "public"."region" VALUES ('6780','迁西县','130227'); INSERT INTO "public"."region" VALUES ('6781','玉田县','130229'); INSERT INTO "public"."region" VALUES ('6782','遵化市','130281'); INSERT INTO "public"."region" VALUES ('6783','迁安市','130283'); INSERT INTO "public"."region" VALUES ('6784','秦皇岛市','1303'); INSERT INTO "public"."region" VALUES ('6785','6784','海港区','130302'); INSERT INTO "public"."region" VALUES ('6786','山海关区','130303'); INSERT INTO "public"."region" VALUES ('6787','北戴河区','130304'); INSERT INTO "public"."region" VALUES ('6788','青龙满族自治县','130321'); INSERT INTO "public"."region" VALUES ('6789','昌黎县','130322'); INSERT INTO "public"."region" VALUES ('6790','抚宁县','130323'); INSERT INTO "public"."region" VALUES ('6791','卢龙县','130324'); INSERT INTO "public"."region" VALUES ('6792','邯郸市','1304'); INSERT INTO "public"."region" VALUES ('6793','6792','邯山区','130402'); INSERT INTO "public"."region" VALUES ('6794','丛台区','130403'); INSERT INTO "public"."region" VALUES ('6795','复兴区','130404'); INSERT INTO "public"."region" VALUES ('6796','峰峰矿区','130406'); INSERT INTO "public"."region" VALUES ('6797','邯郸县','130421'); INSERT INTO "public"."region" VALUES ('6798','临漳县','130423'); INSERT INTO "public"."region" VALUES ('6799','成安县','130424'); INSERT INTO "public"."region" VALUES ('6800','大名县','130425'); INSERT INTO "public"."region" VALUES ('6801','涉县','130426'); INSERT INTO "public"."region" VALUES ('6802','磁县','130427'); INSERT INTO "public"."region" VALUES ('6803','肥乡县','130428'); INSERT INTO "public"."region" VALUES ('6804','永年县','130429'); INSERT INTO "public"."region" VALUES ('6805','邱县','130430'); INSERT INTO "public"."region" VALUES ('6806','鸡泽县','130431'); INSERT INTO "public"."region" VALUES ('6807','广平县','130432'); INSERT INTO "public"."region" VALUES ('6808','馆陶县','130433'); INSERT INTO "public"."region" VALUES ('6809','魏县','130434'); INSERT INTO "public"."region" VALUES ('6810','曲周县','130435'); INSERT INTO "public"."region" VALUES ('6811','武安市','130481'); INSERT INTO "public"."region" VALUES ('6812','邢台市','1305'); INSERT INTO "public"."region" VALUES ('6813','6812','桥东区','130502'); INSERT INTO "public"."region" VALUES ('6814','130503'); INSERT INTO "public"."region" VALUES ('6815','邢台县','130521'); INSERT INTO "public"."region" VALUES ('6816','临城县','130522'); 总结

以上是内存溢出为你收集整理的PostgreSQL的递归查询全部内容,希望文章能够帮你解决PostgreSQL的递归查询所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1171108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存