Using mysql to realize the connect by function of oracle
This article mainly introduces the use of mysql to achieve oracle connect by function methods, the contents of the article are carefully selected and edited by the author, with a certain pertinence, for everyone's reference significance is still relatively great, the following with the author to understand the use of mysql to achieve oracle connect by function method.
1. Generate test tables and data:
Create table mytest
(
Id int primary key
Nodename varchar (20)
Pid int
);
Insert into mytest (id,nodename,pid) values (2, 'Bamboo, 1)
Insert into mytest (id,nodename,pid) values (4, 'Downs, 2)
Insert into mytest (id,nodename,pid) values (5, 'Egg, 2)
Insert into mytest (id,nodename,pid) values (6, 'Fitch, 3)
Insert into mytest (id,nodename,pid) values (8, 'Houses, 0)
Insert into mytest (id,nodename,pid) values (9, 'Irene, 8)
Insert into mytest (id,nodename,pid) values (10, 'Jacks, 8)
Insert into mytest (id,nodename,pid) values (12, 'Little, 9)
Insert into mytest (id,nodename,pid) values (13, 'Manners, 9)
Insert into mytest (id,nodename,pid) values (14, 'Numart 12)
Insert into mytest (id,nodename,pid) values (15, 'Olympiad 12)
Insert into mytest (id,nodename,pid) values (17,'QQ id,nodename,pid 15)
two。 Create a function:
Delimiter $$
CREATE FUNCTION `getParentList` (rootId VARCHAR (50)) RETURNS VARCHAR (1000)
BEGIN
DECLARE sParentList VARCHAR (1000)
DECLARE sParentTemp VARCHAR (1000)
SET sParentTemp = CAST (rootId AS CHAR)
WHILE sParentTemp IS NOT NULL DO
IF (sParentList IS NOT NULL) THEN
SET sParentList = CONCAT (sParentTemp,'/',sParentList)
ELSE
SET sParentList = CONCAT (sParentTemp)
END IF
SELECT GROUP_CONCAT (pid) INTO sParentTemp FROM mytest WHERE FIND_IN_SET (id,sParentTemp) > 0
END WHILE
RETURN sParentList
END$$
DELIMITER
3. Test verification:
Set global log_bin_trust_function_creators=1
SELECT id,pid,getParentList (id) FROM mytest
Mysql > SELECT id,pid,getParentList (id) FROM mytest
+-- +
| | id | pid | getParentList (id) |
+-- +
| | 1 | 0 | 0 amp 1 |
| | 2 | 1 | 0 / 1 / 2 |
| | 4 | 2 | 0qqqlash 2ap4 |
| | 5 | 2 | 0amp 1amp 2Accord 5 |
| | 6 | 3 | 0 / 1 / 3 | 3 / 6 |
| | 7 | 6 | 0qqq1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| | 8 | 0 | 0 amp 8 |
| | 9 | 8 | 0 / 8 / 8 / 9 |
| | 10 | 8 | 0 / 8 | 8 / 10 |
| | 11 | 8 | 0 / 8 / 8 / 11 |
| | 13 | 9 | 0ram, 8amp, 9tick | |
| | 15 | 12 | 0qq8 / 9x12 / 15 |
| | 16 | 15 | 0qq8 / 9x12 / 15 / 16 |
| | 17 | 15 | 0qq8 / 9x12 / 15 / 17 |
+-- +
17 rows in set (0.01 sec)