- ORACLE树形表结构查询 START WITH CONNECT BY PRIOR
-- 1 建表
CREATE TABLE tmp ( code varchar(255), name varchar(255), parent_code varchar(255) );
-- 2插入数据
INSERT INTO tmp VALUES ('01', '河南', NULL);
INSERT INTO tmp VALUES ('0101', '郑州', '01');
INSERT INTO tmp VALUES ('0102', '开封', '01');
INSERT INTO tmp VALUES ('0103', '洛阳', '01');
INSERT INTO tmp VALUES ('02', '上海', NULL);
INSERT INTO tmp VALUES ('0201', '徐汇区', '02');
INSERT INTO tmp VALUES ('0202', '嘉定区', '02');
INSERT INTO tmp VALUES ('010101', '中原区', '0101');
INSERT INTO tmp VALUES ('010102', '管城区', '0101');
INSERT INTO tmp VALUES ('020201', '安亭镇', '0202');
INSERT INTO tmp VALUES ('020202', '嘉定镇', '0202');
INSERT INTO tmp VALUES ('02020101', '黄渡镇', '020201');
-- 3遍历树状数据
-- 向下
SELECT * FROM tmp START WITH code = '0101' CONNECT BY PRIOR code = parent_code ;
-- 向上
SELECT * FROM tmp START WITH code = '0101' CONNECT BY code = PRIOR parent_code ;