USEGEAR

导航

pg获取tree完整的数据

一个表:

DRGCode

DRGParend

DRGName

这样一个tree表结构。

任意查询string,希望得到一个查询完整的tree。怎么做?

SELECT * FROM "DRG"."ADRG" WHERE  "DRGCode" IN
(
WITH RECURSIVE T AS (
SELECT * FROM "DRG"."ADRG" WHERE "DRGCode" ILIKE :P1 OR "DRGName" ILIKE :P1
UNION
SELECT B1.* FROM "DRG"."ADRG" B1,T WHERE B1."DRGParent" = T."DRGCode"
)
SELECT "DRGCode" FROM T
)

OR
 "DRGCode" IN
 (
WITH RECURSIVE T1 AS (

SELECT * FROM "DRG"."ADRG" WHERE "DRGCode" ILIKE :P1 OR "DRGName" ILIKE :P1
UNION
SELECT B.* FROM "DRG"."ADRG" B,T1 WHERE B."DRGCode" = T1."DRGParent"   --AND B."DRGCode" = '37.5100'

)
SELECT "DRGCode" FROM T1

)

 

posted on 2022-02-22 15:15  USEGEAR  阅读(119)  评论(0编辑  收藏  举报