Home > database >  How to realize the cursor traverses the function?
How to realize the cursor traverses the function?

Time:10-09

 CREATE TABLE ` folder ` (
` idFolder ` int (10) DEFAULT NULL,
` FolderName ` varchar (20) DEFAULT NULL,
` idFolderParent ` varchar (10) DEFAULT NULL
);

Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 1 ', '3', '8');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 2 ', '8', '9');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 3 ', '9', '11');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 4 ', '9', '13');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 5 ', '13', '16');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 6 ', '13', '18');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 7 ', '18', '20');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 8 ', '18', '25');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 9 ', '25', '32);
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 10 ', '32', '36');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 11 ', '32', '45');
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 12 ', '25', '47);
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 13 ', '47', '49).
Insert into ` folder ` (` idFolder `, ` FolderName `, ` idFolderParent `) values (' 14 'and' 47 ', '50');


Take idFolderParent column values traversal FolderName column equal records, if a match is found in the record has been through, not just display the current record, want to get the following results:

 FolderName idFolderParent 
3 8
8 50
9 11
13 16
18 to 20
25 36
32 45
47 to 49


The following functions is not I want the results

 
DELIMITER $$
The CREATE DEFINER=` root ` @ % ` ` FUNCTION ` getList ` (rootId varchar (100)) RETURNS a varchar (1000) CHARSET utf8
The BEGIN
DECLARE sTemp VARCHAR (1000);
DECLARE sTempPar VARCHAR (1000);
The SET sTemp=' ';
The SET sTempPar=rootId;

# cycle recursion
WHILE sTempPar is not null DO
The SET sTemp=concat (, sTempPar sTemp, ', ');
The SELECT group_concat (foldername) INTO sTempPar FROM folder where foldername<> Idfolderparent and FIND_IN_SET (idfolderparent sTempPar) & gt; 0;
END the WHILE;

RETURN sTemp.
END $$
DELIMITER.



Select * from the folder where FIND_IN_SET (foldername, getList (' 8 '));
  • Related