CREATE TABLE building (
b_no serial NOT NULL,
b_cd varchar(30) NOT NULL,
b_en_nm varchar(100) NOT NULL,
b_kr_nm varchar(100) NULL,
mineral int4 NULL,
gas int4 NULL,
lv int4 NULL,
p_no int4 NULL,
species bpchar(1) NULL,
"TYPE" varchar(30) NULL,
del_yn bpchar(1) NULL DEFAULT 'N'::bpchar,
use_yn bpchar(1) NULL DEFAULT 'Y'::bpchar,
CONSTRAINT pk_building PRIMARY KEY (b_no),
CONSTRAINT uq_building UNIQUE (b_cd)
);
테이블 데이터 |
3. 쿼리 작성 및 수행
with recursive TECH_TREE as (
select B_NO,
B_CD,
P_NO,
LV
from building
-- where B_NO = 1 -- 최상위 트리 지정
union
select B.B_NO,
B.B_CD,
B.P_NO,
B.LV
from building b
inner join TECH_TREE T
on T.B_NO = B.P_NO
)
select T.B_NO as CHILD_NO,
T.B_CD as CHILD_CD,
T.LV as CHILD_LV,
T.P_NO as PARENT_NO,
B.B_CD as PARENT_CD
from TECH_TREE T
LEFT join building B
on B.B_NO = T.P_NO
order by T.LV, T.P_NO;
쿼리 결과 |
4. TODO
- LV 값에 따라 화면에 들여쓰기,, 접기, 펼치기 등 구현
※ 참고 사이트
https://www.postgresqltutorial.com/postgresql-recursive-query/
https://liquipedia.net/starcraft/Technology_tree
https://kugancity.tistory.com/entry/postgresql-%EC%9D%98-%EA%B8%B0%EB%B3%B8%ED%82%A4-%EC%9E%90%EB%8F%99-%EC%A6%9D%EA%B0%80