ORACLE-递归查询、树操作

1. 数据准备

-- 测试数据准备
DROP TABLE untifa_test;CREATE TABLE untifa_test(child_id NUMBER(10) NOT NULL, --子idtitle VARCHAR2(50), --标题relation_type VARCHAR(10) --关系,parent_id NUMBER(10) --父id
);insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (1, '父菜单1', 'A', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (1, '父菜单1', 'B', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (1, '父菜单1', 'C', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (2, '父菜单2', 'A', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (2, '父菜单2', 'B', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (2, '父菜单2', 'C', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (3, '父菜单3', 'A', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (3, '父菜单3', 'B', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (3, '父菜单3', 'C', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (4, '父菜单4', 'A', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (4, '父菜单4', 'B', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (4, '父菜单4', 'C', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (5, '父菜单5', 'A', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (5, '父菜单5', 'B', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (5, '父菜单5', 'C', 0);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (6, '一级菜单6', 'A', 1);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (7, '一级菜单7', 'B', 1);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (8, '一级菜单8', 'C', 1);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (9, '一级菜单9', 'A', 2);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (10, '一级菜单10', 'B', 2);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (11, '一级菜单11', 'C', 2);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (12, '一级菜单12', 'A', 3);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (13, '一级菜单13', 'B', 3);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (14, '一级菜单14', 'C', 3);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (15, '一级菜单15', 'A', 4);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (16, '一级菜单16', 'B', 4);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (17, '一级菜单17', 'C', 4);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (18, '一级菜单18', 'A', 5);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (19, '一级菜单19', 'B', 5);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (20, '一级菜单20', 'C', 5);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (21, '二级菜单21', 'A', 6);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (22, '二级菜单22', 'B', 6);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (23, '二级菜单23', 'C', 6);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (24, '二级菜单24', 'A', 7);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (25, '二级菜单25', 'B', 7);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (26, '二级菜单26', 'C', 7);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (27, '二级菜单27', 'A', 8);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (28, '二级菜单28', 'B', 8);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (29, '二级菜单29', 'C', 8);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (30, '二级菜单30', 'A', 9);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (31, '二级菜单31', 'B', 9);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (32, '二级菜单32', 'C', 9);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (33, '二级菜单33', 'A', 10);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (34, '二级菜单34', 'B', 10);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (35, '二级菜单35', 'C', 10);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (36, '二级菜单36', 'A', 11);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (37, '二级菜单37', 'B', 11);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (38, '二级菜单38', 'C', 11);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (39, '二级菜单39', 'A', 12);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (40, '二级菜单40', 'B', 12);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (41, '二级菜单41', 'C', 12);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (42, '二级菜单42', 'A', 13);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (43, '二级菜单43', 'B', 13);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (44, '二级菜单44', 'C', 13);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (45, '二级菜单45', 'A', 14);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (46, '二级菜单46', 'B', 14);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (47, '二级菜单47', 'C', 14);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (48, '二级菜单48', 'A', 15);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (49, '二级菜单49', 'B', 15);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (50, '二级菜单50', 'C', 15);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (51, '二级菜单51', 'A', 16);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (52, '二级菜单52', 'B', 16);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (53, '二级菜单53', 'C', 16);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (54, '二级菜单54', 'A', 17);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (55, '二级菜单55', 'B', 17);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (56, '二级菜单56', 'C', 17);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (57, '二级菜单57', 'A', 18);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (58, '二级菜单58', 'B', 18);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (59, '二级菜单59', 'C', 18);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (60, '二级菜单60', 'A', 19);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (61, '二级菜单61', 'B', 19);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (62, '二级菜单62', 'C', 19);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (63, '二级菜单63', 'A', 20);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (64, '二级菜单64', 'B', 20);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (65, '二级菜单65', 'C', 20);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (66, '三级菜单66', 'A', 21);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (67, '三级菜单67', 'B', 21);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (68, '三级菜单68', 'C', 21);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (69, '三级菜单69', 'A', 22);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (70, '三级菜单70', 'B', 22);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (71, '三级菜单71', 'C', 22);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (72, '三级菜单72', 'A', 23);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (73, '三级菜单73', 'B', 23);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (74, '三级菜单74', 'C', 23);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (75, '三级菜单75', 'A', 24);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (76, '三级菜单76', 'B', 24);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (77, '三级菜单77', 'C', 24);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (78, '三级菜单78', 'A', 25);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (79, '三级菜单79', 'B', 25);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (80, '三级菜单80', 'C', 25);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (81, '三级菜单81', 'A', 26);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (82, '三级菜单82', 'B', 26);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (83, '三级菜单83', 'C', 26);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (84, '三级菜单84', 'A', 27);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (85, '三级菜单85', 'B', 27);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (86, '三级菜单86', 'C', 27);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (87, '三级菜单87', 'A', 28);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (88, '三级菜单88', 'B', 28);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (89, '三级菜单89', 'C', 28);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (90, '三级菜单90', 'A', 29);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (91, '三级菜单91', 'B', 29);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (92, '三级菜单92', 'C', 29);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (93, '三级菜单93', 'A', 30);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (94, '三级菜单94', 'B', 30);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (95, '三级菜单95', 'C', 30);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (96, '三级菜单96', 'A', 31);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (97, '三级菜单97', 'B', 31);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (98, '三级菜单98', 'C', 31);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (99, '三级菜单99', 'A', 32);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (100, '三级菜单100', 'B', 32);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (101, '三级菜单101', 'C', 32);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (102, '三级菜单102', 'A', 33);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (103, '三级菜单103', 'B', 33);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (104, '三级菜单104', 'C', 33);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (105, '三级菜单105', 'A', 34);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (106, '三级菜单106', 'B', 34);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (107, '三级菜单107', 'C', 34);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (108, '三级菜单108', 'A', 35);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (109, '三级菜单109', 'B', 35);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (110, '三级菜单110', 'C', 35);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (111, '三级菜单111', 'A', 36);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (112, '三级菜单112', 'B', 36);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (113, '三级菜单113', 'C', 36);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (114, '三级菜单114', 'A', 37);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (115, '三级菜单115', 'B', 37);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (116, '三级菜单116', 'C', 37);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (117, '三级菜单117', 'A', 38);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (118, '三级菜单118', 'B', 38);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (119, '三级菜单119', 'C', 38);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (120, '三级菜单120', 'A', 39);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (121, '三级菜单121', 'B', 39);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (122, '三级菜单122', 'C', 39);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (123, '三级菜单123', 'A', 40);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (124, '三级菜单124', 'B', 40);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (125, '三级菜单125', 'C', 40);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (126, '三级菜单126', 'A', 41);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (127, '三级菜单127', 'B', 41);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (128, '三级菜单128', 'C', 41);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (129, '三级菜单129', 'A', 42);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (130, '三级菜单130', 'B', 42);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (131, '三级菜单131', 'C', 42);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (132, '三级菜单132', 'A', 43);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (133, '三级菜单133', 'B', 43);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (134, '三级菜单134', 'C', 43);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (135, '三级菜单135', 'A', 44);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (136, '三级菜单136', 'B', 44);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (137, '三级菜单137', 'C', 44);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (138, '三级菜单138', 'A', 45);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (139, '三级菜单139', 'B', 45);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (140, '三级菜单140', 'C', 45);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (141, '三级菜单141', 'A', 46);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (142, '三级菜单142', 'B', 46);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (143, '三级菜单143', 'C', 46);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (144, '三级菜单144', 'A', 47);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (145, '三级菜单145', 'B', 47);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (146, '三级菜单146', 'C', 47);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (147, '三级菜单147', 'A', 48);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (148, '三级菜单148', 'B', 48);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (149, '三级菜单149', 'C', 48);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (150, '三级菜单150', 'A', 49);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (151, '三级菜单151', 'B', 49);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (152, '三级菜单152', 'C', 49);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (153, '三级菜单153', 'A', 50);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (154, '三级菜单154', 'B', 50);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (155, '三级菜单155', 'C', 50);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (156, '三级菜单156', 'A', 51);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (157, '三级菜单157', 'B', 51);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (158, '三级菜单158', 'C', 51);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (159, '三级菜单159', 'A', 52);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (160, '三级菜单160', 'B', 52);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (161, '三级菜单161', 'C', 52);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (162, '三级菜单162', 'A', 53);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (163, '三级菜单163', 'B', 53);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (164, '三级菜单164', 'C', 53);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (165, '三级菜单165', 'A', 54);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (166, '三级菜单166', 'B', 54);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (167, '三级菜单167', 'C', 54);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (168, '三级菜单168', 'A', 55);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (169, '三级菜单169', 'B', 55);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (170, '三级菜单170', 'C', 55);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (171, '三级菜单171', 'A', 56);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (172, '三级菜单172', 'B', 56);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (173, '三级菜单173', 'C', 56);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (174, '三级菜单174', 'A', 57);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (175, '三级菜单175', 'B', 57);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (176, '三级菜单176', 'C', 57);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (177, '三级菜单177', 'A', 58);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (178, '三级菜单178', 'B', 58);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (179, '三级菜单179', 'C', 58);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (180, '三级菜单180', 'A', 59);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (181, '三级菜单181', 'B', 59);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (182, '三级菜单182', 'C', 59);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (183, '三级菜单183', 'A', 60);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (184, '三级菜单184', 'B', 60);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (185, '三级菜单185', 'C', 60);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (186, '三级菜单186', 'A', 61);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (187, '三级菜单187', 'B', 61);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (188, '三级菜单188', 'C', 61);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (189, '三级菜单189', 'A', 62);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (190, '三级菜单190', 'B', 62);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (191, '三级菜单191', 'C', 62);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (192, '三级菜单192', 'A', 63);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (193, '三级菜单193', 'B', 63);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (194, '三级菜单194', 'C', 63);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (195, '三级菜单195', 'A', 64);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (196, '三级菜单196', 'B', 64);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (197, '三级菜单197', 'C', 64);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (198, '三级菜单198', 'A', 65);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (199, '三级菜单199', 'B', 65);
insert into untifa_test (CHILD_ID, TITLE, RELATION_TYPE, PARENT_ID)values (200, '三级菜单200', 'C', 65);

1. 树状关系图

在这里插入图片描述

2. 语法

SELECT colnum FROM tablename WHERE 条件1
  START WITH 条件2
  CONNECT BY [NOCYCLE] 条件3(PRIOR 列名1 = 列名2 | 列名1 = PRIOR 列名2 …)


WHERE 条件1 过滤条件语句,对树状结构遍历后得到的记录进行过滤。
START WITH 条件2 限定条件语句,限定根节点,可以将跟节点限制为1个或多个。
CONNECT BY 条件3(PRIOR 列名1 = 列名2 | 列名1 = PRIOR 列名2) 连接条件语句,其中PRIOR表示的是上一条记录。
NOCYCLE 参数避免进入死循环


比如 CONNECT BY PRIOR child_id = parent_id 表示上一条记录的child_id是本条记录的parent_id,即本条记录的父节点是上一条记录的子节点。
CONNCET BY 说明每行数据是按层次检索,并规定将表中的数据链入树形结构的关系中。
PRIOR在=的左侧,代表左侧的为父节点,否则右侧为父节点,这决定了遍历树的方向。


执行顺序

  1. 根据START WITH 条件2 确定要访问的根节点有哪些
  2. 访问此节点
  3. 判断此节点有无未访问的子节点,若有,则转向最左侧的未被访问的子节点,并执行步骤2、步骤3,否则执行步骤4
  4. 若该节点为根节点,则访问完毕,否则执行步骤5
  5. 返回到此节点的父节点,并执行步骤3
  6. 根据where 条件1 过滤最终遍历树后的查询结果

3. 适用函数

  • LPAD
    LPAD(STR1 =>
    , LEN =>
    , PAD => )
    STR1 准备被填充的字符串,长度不能为空–为空时无效果;
    LEN 填充之后的字符串长度,也就是该函数返回的字符串长度,如果这个数量比原字符串的长度要短,lpad函数将会把字符串截取成从左到右的n个字符
    PAD 填充字符串,是个可选参数,这个字符串是要粘贴到string的左边,如果这个参数未写,lpad函数将会在string的左边粘贴空格

  • LEVEL
    LEVEL 树状关系的层级,根节点的层级始终为1

  • connect_by_isleaf
    此函数用来判断当前节点是否包含下级节点,如果包含的话,说明不是叶子节点,返回0;反之,如果不包含下级节点,这里返回1

  • sys_connect_by_path(colnum,‘str’)
    此函数将遍历到的路径根据函数中的分隔符,组成一个新的字符串

  • WITH tablename AS () SELECT … FROM tablename WHERE …;
    ()中写查询的逻辑,将查询到的结果放入临时表tablename中,再根据条件按需查询

4. 查询案例

4.1 案例1


-- 想要得到从父节点1开始,关系级别是A的所有子节点
SELECT LPAD('#', 2 * (LEVEL - 1) + 1, '-') || t.title,LEVEL,connect_by_isleaf,sys_connect_by_path(t.title || t.relation_type, '/'),t.*FROM untifa_test tSTART WITH t.child_id = 1AND t.relation_type = 'A' -- 限定跟结果集
CONNECT BY NOCYCLE PRIOR t.child_id = t.parent_idAND t.relation_type = 'A' -- 限定子节点结果集ORDER BY LEVEL;

查询结果:
在这里插入图片描述

4.2 案例2


-- 想要得到从子节点66开始,关系级别是A的所有父节点
SELECT LPAD('#', 2 * (LEVEL - 1) + 1, '-') || t.title,LEVEL,connect_by_isleaf,sys_connect_by_path(t.title || t.relation_type, '/'),t.*FROM untifa_test tSTART WITH t.child_id = 66AND t.relation_type = 'A' -- 限定跟结果集
CONNECT BY NOCYCLE t.child_id = PRIOR t.parent_idAND t.relation_type = 'A' -- 限定子节点结果集ORDER BY LEVEL;

查询结果:
在这里插入图片描述

4.3 案例3


-- 将父节点1A的parent_id改为1
UPDATE untifa_test t SET t.parent_id = '1' WHERE t.child_id = '1' AND t.relation_type = 'A';
-- 再查询从父节点1开始,关系级别是A的所有子节点
SELECT LPAD('#', 2 * (LEVEL - 1) + 1, '-') || t.title,LEVEL,connect_by_isleaf,sys_connect_by_path(t.title || t.relation_type, '/'),t.*FROM untifa_test tSTART WITH t.child_id = 1AND t.relation_type = 'A'
CONNECT BY PRIOR t.child_id = t.parent_idAND t.relation_type = 'A'ORDER BY LEVEL;

查询结果:
在这里插入图片描述
这是因为父节点的child_id与parent_id都为1,形成了死循环,所有我们要加入NOCYCLE来规避死循环

SELECT LPAD('#', 2 * (LEVEL - 1) + 1, '-') || t.title,LEVEL,connect_by_isleaf,sys_connect_by_path(t.title || t.relation_type, '/'),t.*FROM untifa_test tSTART WITH t.child_id = 1AND t.relation_type = 'A'
CONNECT BY NOCYCLE PRIOR t.child_id = t.parent_idAND t.relation_type = 'A'ORDER BY LEVEL;

查询结果:
在这里插入图片描述

4.4 案例4


-- 想要得到从子节点66开始,关系级别是A的所有父节点
-- 同案例3一样 CONNECT BY 后面不加 NOCYCLE 则会陷入死循环报错
SELECT LPAD('#', 2 * (LEVEL - 1) + 1, '-') || t.title,LEVEL,connect_by_isleaf,sys_connect_by_path(t.title || t.relation_type, '/'),t.*FROM untifa_test tSTART WITH t.child_id = 66AND t.relation_type = 'A'
CONNECT BY NOCYCLE t.child_id = PRIOR t.parent_idAND t.relation_type = 'A'ORDER BY LEVEL;

查询结果:
在这里插入图片描述

对比案例4与案例2的结果不难发现,在将child_id为1的parent_id从0改为1后,查询结果少了一条,还没弄清楚原因,猜测是因为死循环,过滤掉了parend_id=1和child_id=1的记录,但是不清楚为什么案例3没有过滤掉parend_id=1和child_id=1的记录。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/173960.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CAP定理下:Zookeeper、Eureka、Nacos简单分析

CAP定理下:Zookeeper、Eureka、Nacos简单分析 CAP定理 C: 一致性(Consistency):写操作之后的读操作也需要读到之前的 A: 可用性(Availability):收到用户请求,服务器就必须给出响应 P…

CSS3中的字体和文本样式

CSS3优化了CSS 2.1的字体和文本属性,同时新增了各种文字特效,使网页文字更具表现力和感染力,丰富了网页设计效果,如自定义字体类型、更多的色彩模式、文本阴影、生态生成内容、各种特殊值、函数等。 1、字体样式 字体样式包括类…

MinIO安装

Minio是一个开源的分布式对象存储服务器,它兼容Amazon S3服务接口。它可以用于构建私有云存储,为应用程序提供可扩展的对象存储功能。 安装 docker安装 docker run -d -p 9000:9000 -p 50000:50000 --name minio \ -e "MINIO_ROOT_USERadminpili…

Hadoop3.0大数据处理学习1(Haddop介绍、部署、Hive部署)

Hadoop3.0快速入门 学习步骤: 三大组件的基本理论和实际操作Hadoop3的使用,实际开发流程结合具体问题,提供排查思路 开发技术栈: Linux基础操作、Sehll脚本基础JavaSE、Idea操作MySQL Hadoop简介 Hadoop是一个适合海量数据存…

asp.net学生考试报名管理系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio

一、源码特点 asp.net学生考试报名管理系统是一套完善的web设计管理系统系统,系统具有完整的源代码和数据库,系统主要采用B/S模式开发。开发环境为vs2010,数据库为sqlserver2008,使 用c#语言开发 应用技术:asp…

机器学习---使用 TensorFlow 构建神经网络模型预测波士顿房价和鸢尾花数据集分类

1. 预测波士顿房价 1.1 导包 from __future__ import absolute_import from __future__ import division from __future__ import print_functionimport itertoolsimport pandas as pd import tensorflow as tftf.logging.set_verbosity(tf.logging.INFO) 最后一行设置了Ten…

vue中使用xlsx插件导出多sheet excel实现方法

安装xlsx,一定要注意版本: npm i xlsx0.17.0 -S package.json: {"name": "hello-world","version": "0.1.0","private": true,"scripts": {"serve": "vue-c…

ESM蛋白质语言模型系列

模型总览 第一篇《Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences 》ESM-1b 第二篇《MSA Transformer》在ESM-1b的基础上作出改进,将模型的输入从单一蛋白质序列改为MSA矩阵,并在Tran…

RK3568-适配at24c04模块

将at24c04模块连接到开发板i2c2总线上 i2ctool查看i2c2总线上都有哪些设备 UU表示设备地址的从设备被驱动占用,卸载对应的驱动后,UU就会变成从设备地址。at24c04模块设备地址 0x50和0x51是at24c04模块i2c芯片的设备地址。这个从芯片手册上也可以得知。A0 A1 A2表示的是模块对…

简单而高效:使用PHP爬虫从网易音乐获取音频的方法

概述 网易音乐是一个流行的在线音乐平台,提供了海量的音乐资源和服务。如果你想从网易音乐下载音频文件,你可能会遇到一些困难,因为网易音乐对其音频资源进行了加密和防盗链的处理。本文将介绍一种使用PHP爬虫从网易音乐获取音频的方法&…

Go学习第十六章——Gin文件上传与下载

Go web框架——Gin文件上传与下载 1. 文件上传1.1 入门案例(单文件)1.2 服务端保存文件的几种方式SaveUploadedFileCreateCopy 1.3 读取上传的文件1.4 多文件上传 2. 文件下载2.1 快速入门2.2 前后端模式下的文件下载2.3 中文乱码问题 1. 文件上传 1.1 …

lesson2(补充)关于>>运算符和<<运算符重载

个人主页&#xff1a;Lei宝啊 愿所有美好如期而遇 前言&#xff1a; cout和cin我们在使用时需要包含iostream头文件&#xff0c;我们可以知道的是cout是写在ostream类里的&#xff0c;cin是写在istream类里的&#xff0c;他们都是定义出的对象&#xff0c;而<< 和 >…

M1安装OpenPLC Editor

下载OpenPLC Editor for macOS.zip文件后&#xff0c;使用tar -zvxf命令解压&#xff0c;然后将"OpenPLC Editor"拖入到"应用程序"文件夹 右键点击"OpenPLC Editor"&#xff0c;打开这个""文件&#xff0c;替换为以下内容 #!/bin/bash…

香港服务器如何做负载均衡?

​  在现代互联网时代&#xff0c;随着网站访问量的不断增加&#xff0c;服务器的负载也越来越重。为了提高网站的性能和可用性&#xff0c;负载均衡成为了一种常见的解决方案。 什么是负载均衡? 负载均衡是一种技术解决方案&#xff0c;用于在多个服务器之间分配负载&#…

搜维尔科技:【应用】配备MTi-3的轻便型ROV,在水下进行地理标记视觉检测

部署潜水员进行水下摄像&#xff0c;不仅难度高而且费用昂贵&#xff0c;需要受过潜水和摄像两方面培训的专业人员来进行。但有些水下作业任务例如拍摄海底管道内部的照片&#xff0c;由于人员无法进入或危险度高的原因&#xff0c;无法由潜水员完成。 如今&#xff0c;俄罗…

vue源码分析(五)——vue render 函数的使用

文章目录 前言一、render函数1、render函数是什么&#xff1f; 二、render 源码分析1.执行initRender方法2.vm._c 和 vm.$createElement 调用 createElement 方法详解&#xff08;1&#xff09;区别&#xff08;2&#xff09;代码 3、原型上的_render方法&#xff08;1&#xf…

JWT详解解读读

&#x1f4d1;前言 本文主要是jwt解读文章&#xff0c;如果有什么需要改进的地方还请大佬指出⛺️ &#x1f3ac;作者简介&#xff1a;大家好&#xff0c;我是青衿&#x1f947; ☁️博客首页&#xff1a;CSDN主页放风讲故事 &#x1f304;每日一句&#xff1a;努力一点&#…

数据结构-初识泛型

写在前&#xff1a; 这一篇博客主要来初步的记录以下泛型的相关内容&#xff0c;内容比较琐碎&#xff0c;就不进行目录的整合&#xff0c;后续可能会对泛型这里进行系统性的梳理&#xff0c;此篇博客主要是对泛型有一个简单的认识与理解&#xff0c;需要知晓的内容。 当我调用…

七层负载均衡 HAproxy

一、HAproxy 1、负载均衡类型&#xff1a; (1) 无负载均衡&#xff1a; 没有负载均衡&#xff0c;用户直接连接到 Web 服务器。当许多用户同时访问服务器时&#xff0c;可能无法连接。 (2) 四层负载均衡&#xff1a; 用户访问负载均衡器&#xff0c;负载均衡器将用户的请求…

听GPT 讲Rust源代码--library/std(8)

题图来自Why is Rust programming language so popular?[1] File: rust/library/std/src/sys/sgx/abi/reloc.rs 在Rust源代码中&#xff0c;sgx/abi/reloc.rs文件的作用是定义了针对Intel Software Guard Extensions (SGX)的重定位相关结构和函数。 该文件中的Rela 结构定义了…