----創(chuàng)建數(shù)據(jù)
為牟平等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及牟平網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站制作、成都網(wǎng)站設計、牟平網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!1.1、建立表與插入數(shù)據(jù)
CREATE TABLE DISTRICT ( ID NUMBER(10) NOT NULL, PARENT_ID NUMBER(10), NAME VARCHAR2(255 BYTE) NOT NULL);ALTER TABLE DISTRICT ADD ( CONSTRAINT DISTRICT_PK PRIMARY KEY (ID));ALTER TABLE DISTRICT ADD ( CONSTRAINT DISTRICT_R01 FOREIGN KEY (PARENT_ID) REFERENCES DISTRICT (ID)); insert into DISTRICT (id, parent_id, name)values (1, null, '四川省');insert into DISTRICT (id, parent_id, name)values (2, 1, '巴中市');insert into DISTRICT (id, parent_id, name)values (3, 1, '達州市');insert into DISTRICT (id, parent_id, name)values (4, 2, '巴州區(qū)');insert into DISTRICT (id, parent_id, name)values (5, 2, '通江縣');insert into DISTRICT (id, parent_id, name)values (6, 2, '平昌縣');insert into DISTRICT (id, parent_id, name)values (7, 3, '通川區(qū)');insert into DISTRICT (id, parent_id, name)values (8, 3, '宣漢縣');insert into DISTRICT (id, parent_id, name)values (9, 8, '塔河鄉(xiāng)');insert into DISTRICT (id, parent_id, name)values (10, 8, '三河鄉(xiāng)');insert into DISTRICT (id, parent_id, name)values (11, 8, '胡家鎮(zhèn)');insert into DISTRICT (id, parent_id, name)values (12, 8, '南壩鎮(zhèn)');insert into DISTRICT (id, parent_id, name)values (13, 6, '大寨鄉(xiāng)');insert into DISTRICT (id, parent_id, name)values (14, 6, '響灘鎮(zhèn)');insert into DISTRICT (id, parent_id, name)values (15, 6, '龍崗鎮(zhèn)');insert into DISTRICT (id, parent_id, name)values (16, 6, '白衣鎮(zhèn)');commit;
查詢所有子節(jié)點
SELECT *
FROM district
START WITH NAME ='巴中市'
CONNECT BY PRIOR ID=parent_id
2.2、查詢所有父節(jié)點
SELECT *
FROM district
START WITH NAME ='平昌縣'
CONNECT BY PRIOR parent_id=ID
只需要交換 id 與parent_id的位置即可
2.3、查詢指定節(jié)點的,根節(jié)點
SELECT d.*, connect_by_root(d.id), connect_by_root(NAME)FROM district dWHERE NAME='平昌縣'START WITH d.parent_id=1 --d.parent_id is null 結果為四川省CONNECT BY PRIOR d.ID=d.parent_id
2.4、查詢巴中市下行政組織遞歸路徑
SELECT ID,parent_id,NAME, sys_connect_by_path(NAME,'->') namepath,LEVELFROM district START WITH NAME='巴中市'CONNECT BY PRIOR ID=parent_id
3.1、with遞歸子類
WITH t (ID ,parent_id,NAME) --要有列名AS(SELECT ID ,parent_id,NAME FROM district WHERE NAME='巴中市'UNION ALLSELECT d.ID ,d.parent_id,d.NAME FROM t,district d --要指定表和列表,WHERE t.id=d.parent_id )SELECT * FROM t;
3.2、遞歸父類
WITH t (ID ,parent_id,NAME) --要有表AS(SELECT ID ,parent_id,NAME FROM district WHERE NAME='通江縣'UNION ALLSELECT d.ID ,d.parent_id,d.NAME FROM t,district d --要指定表和列表,WHERE t.parent_id=d.id )SELECT * FROM t;
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。