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.mzph.cn/news/121382.shtml

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

相关文章

SpringBoot整合Gateway 的Demo(附源码)

源码&#xff0c;可直接下载 Gateway模块 Gateway 的父pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:sc…

vue核心面试题汇总【查缺补漏】

给大家推荐一个实用面试题库 1、前端面试题库 &#xff08;面试必备&#xff09; 推荐&#xff1a;★★★★★ 地址&#xff1a;web前端面试题库 很喜欢‘万变不离其宗’这句话&#xff0c;希望在不断的思考和总结中找到Vue中的宗&#xff0c;来解答面试官抛出的…

2023MathorCup(妈妈杯) 数学建模挑战赛 解题思路

云顶数模最新解题思路免费分享~~ 2023妈妈杯数学建模A题B题思路&#xff0c;供大家参考~~ A题 B题

对mysql的联合索引的深刻理解

背景 对mysql的联合索引的考察是Java程序员面试高频考点&#xff01;必须深刻理解掌握否则容易丢分非常可惜。 技术难点 考察对最左侧匹配原理理解。 原理 暂且不表。网上讲这非常多。我理解就是&#xff0c;B树每个非叶子节点的值都是有序存放索引的值。 比如对A、B、C …

物联网和互联网医院小程序:如何实现医疗设备的远程监测和管理?

物联网&#xff08;IoT&#xff09;技术的发展为医疗设备的远程监测和管理提供了巨大的机会。结合互联网医院小程序&#xff0c;我们可以实现对医疗设备的远程访问、监控和管理&#xff0c;从而提高医疗服务的质量和效率。本文将介绍如何实现医疗设备的远程监测和管理&#xff…

appium操控微信小程序的坑

appium操控微信小程序的坑 打不开启动页面driver的context只有NATIVE_APP小程序上元素找不到 我打算使用appium操控微信小程序&#xff0c;只要能够获取到小程序的页面元素就算成功。下面都是我遇到的问题。 打不开启动页面 以下是我的appium的配置参数和代码&#xff1a; de…

Hyperledger Fabric搭建测试网络

本文使用的Fabric版本&#xff1a;V2.5.4 Ubuntu系统&#xff1a;16.04LTS 前序文章已经详细介绍了如何安装部署Hyperledger Fabric系统&#xff0c;这里不再赘述。本篇文章主要介绍如何使用Fabric的测试网络。在正式开始之前&#xff0c;有一点需要说明&#xff1a; Hyperled…

24 行为型模式-访问者模式

1 访问者模式介绍 访问者模式在实际开发中使用的非常少,因为它比较难以实现并且应用该模式肯能会导致代码的可读性变差,可维护性变差,在没有特别必要的情况下,不建议使用访问者模式。 2 访问者模式原理 3 访问者模式实现 我们以超市购物为例,假设超市中的三类商品: 水果,糖…

JVM(二)

一,运行时数据区 Java虚拟机在运行Java程序过程中管理的内存区域,称之为运行时数据区。 1.1 程序计数器 程序计数器(Program Counter Register)也叫PC寄存器,每个线程会通过程序计数器记录当前要执行的的字节码指令的地址。 在加载阶段,虚拟机将字节码文件中的指令读取…

多线程面试相关知识点

文章目录 (一) 进程线程和协程的区别创建线程的4种方式1. 继承Thread类2. 实现runnable接口3. 实现Callable接口4. 线程池创建 runnable 和 callable 有什么区别线程的 run()和 start()有什么区别&#xff1f;线程之间的状态变化notify()和 notifyAll()有什么区别&#xff1f;j…

HPV感染的风险:闫会宁主任分析酒店环境中的常见因素

人类乳头瘤病毒(HPV)是一种普遍存在的病毒&#xff0c;其存在和传播方式多种多样。近年来&#xff0c;人们对于HPV的认识不断深入&#xff0c;知道其在酒店环境中的传播风险。本文将探讨哪些情况下在酒店可能感染HPV。 一、HPV的传播方式 HPV主要通过直接接触传播&#xff0c…

数据湖Iceberg介绍和使用(集成Hive、SparkSQL、FlinkSQL)

文章目录 简介概述作用特性数据存储、计算引擎插件化实时流批一体数据表演化&#xff08;Table Evolution&#xff09;模式演化&#xff08;Schema Evolution&#xff09;分区演化&#xff08;Partition Evolution&#xff09;列顺序演化&#xff08;Sort Order Evolution&…

python:使用Scikit-image对遥感影像进行梯度特征提取(gradient)

作者:CSDN @ _养乐多_ 在本博客中,我们将介绍如何使用Scikit-Image来进行梯度特征提取(gradient),并且提供一个示例代码,演示了如何在单波段遥感图像上应用这些方法。 梯度特征是指用于表示图像中亮度或颜色变化的特征。它包括两个关键成分:梯度幅值和梯度方向。梯度幅…

RabbitMQ (4)

RabbitMQ (4) 文章目录 1. 死信的概念2. 死信的来源3. 死信代码案例3.1 TTL 过期时间3.2 超过队列最大长度3.3 拒绝消息 前言   上文我们已经学习完 交换机 &#xff0c;知道了几个交换机的使用 &#xff0c;下面我们来学习一下 死信队列 1. 死信的概念 先从概念解释上搞清楚这…

【C++】mapset的底层结构 -- AVL树(高度平衡二叉搜索树)

前面我们对 map / multimap / set / multiset 进行了简单的介绍&#xff0c;可以发现&#xff0c;这几个容器有个共同点是&#xff1a;其底层都是按照二叉搜索树来实现的。 但是二叉搜索树有其自身的缺陷&#xff0c;假如往树中插入的元素有序或者接近有序&#xff0c;二叉搜索…

漏洞复现-phpmyadmin_SQL注入 (CVE-2020-5504)

phpmyadmin SQL注入 _&#xff08;CVE-2020-5504&#xff09; 漏洞信息 CVE-2020-5504sql注入漏洞Phpmyadmin 5.00以下 描述 ​ phpMyAdmin是Phpmyadmin团队的一套免费的、基于Web的MySQL数据库管理工具。该工具能够创建和删除数据库&#xff0c;创建、删除、修改数据库表&…

Java学习 5.习题2.

练习题1&#xff1a;判断一个数字是偶数还是奇数 int num110;if(num1%20) {System.out.println("num1是一个偶数");}else{System.out.println("num1是一个奇数");} 练习题2&#xff1a;判断一个数是正数还是负数还是0 int num2-5;if(num2>0) {System.ou…

yarn install 这个命令安装如何加速

yarn install 命令用来安装项目依赖&#xff0c;其速度受多种因素影响&#xff0c;如网络速度、npm/yarn包的源服务器、以及本地缓存等。以下是一些可能帮助你加速 yarn install 的方法&#xff1a; 1. 使用国内镜像 如果你在中国&#xff0c;可以使用淘宝的 npm 镜像&#x…

【Javascript】json

目录 什么是json&#xff1f; 书写格式 json 序列化和反序列化 序列化 反序列化 什么是json&#xff1f; JSON(JavaScript Object Notation)是⼀种轻量级的数据交换格式&#xff0c;它基于JavaScript的⼀个⼦集&#xff0c;易于⼈的编写和阅读&#xff0c;也易于机器解析…

栈、队列、矩阵的总结

栈的应用 括号匹配 表达式求值&#xff08;中缀&#xff0c;后缀&#xff09; 中缀转后缀&#xff08;机算&#xff09; 中缀机算 后缀机算 总结 特殊矩阵 对称矩阵的压缩存储 三角矩阵 三对角矩阵 稀疏矩阵的压缩存储