我想在MYSQL中创建一个过程,它将从我的嵌套类别结构中检索我的数据:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| category_id | int(11) | NO | PRI | NULL | auto_increment |
| category | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| parent_id | int(11) | YES | | 0 | |
| sequence | int(11) | YES | | 0 | |
+-------------+--------------+------+-----+---------+----------------+
示例数据:
CREATE TABLE `categories` (
`category_id` INTEGER(11) NOT NULL AUTO_INCREMENT,
`category` VARCHAR(255),
`description` VARCHAR(255),
`parent_id` INTEGER(11) DEFAULT '0',
`sequence` INTEGER(11) DEFAULT '0',
PRIMARY KEY (`category_id`),
UNIQUE KEY `category_id` (`category_id`)
);
COMMIT;
/* Data for the `categories` table (Records 1 - 14) */
INSERT INTO `categories` (`category_id`, `category`, `description`, `parent_id`, `sequence`) VALUES
(2, 'Student Level', 'Students Level Grade...', 0, 0),
(18, '3ème', NULL, 2, 1),
(19, '5ème', NULL, 2, 2),
(20, '6ème', NULL, 2, 3),
(21, 'Lettres', NULL, 18, 0),
(22, 'Séquence I : En route pour le brevet', NULL, 21, 0),
(23, 'Séquence II : Jeannot et Colin', NULL, 21, 0),
(24, 'Séquence III : De l''éducation', NULL, 21, 0),
(25, 'Séquence IV : Pauline', NULL, 21, 0),
(26, 'Séquence V : Œdipe roi', NULL, 21, 0),
(27, 'Séquence VI : La planète des singes', NULL, 21, 0),
(28, 'Séquence VII : La poésie engagée', NULL, 21, 0),
(29, 'Blog Categories', 'Blog Categories', 0, 0),
(30, 'Blog', 'Default Blog', 29, 0);
我希望创建一个我将调用的函数,例如调用GetFormatCategories()
,它将返回以下字段和值:
字段>值
category_id >其原始category_id值
类别>其原始类别值
序列>原始序列
parent_id >原来的parent_id
级别>例如(学生等级= 1),(3?= 2),(Lettres = 3)(S?quence I:En route pour le brevet = 4)按等级划分的类别等级/ p>
结果应按级别排序,例如:
Level 1
Level 2
Level 3
Level 4
Level 4
Level 4
Level 4
Level 4
Level 3
Level 4
Level 4
Level 2
Level 3
Level 4
Level 4
我尝试过使用各种示例,例如http://rpbouman.blogspot.com/2005/10/nesting-mysql-cursor-loops.html,但我仍有问题:
CREATE DEFINER = 'root'@'localhost' PROCEDURE `catlist`() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY INVOKER COMMENT '' BEGIN -- Done Trigger DECLARE tDone boolean default false; -- Possible Field i Need to Use DECLARE vCatID INT; DECLARE vCatName VARCHAR(255); DECLARE vParentID INT; DECLARE vSequence INT; DECLARE vLevel INT; DECLARE topCursor CURSOR FOR SELECT `category_id`,`category`,`parent_id`,`sequence` FROM `level_category`; DECLARE subCursor CURSOR FOR SELECT `category_id`,`category`,`parent_id`,`sequence` FROM `categories` WHERE parent_id = vCatID; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tDone := TRUE; -- Create Temporary tblResult to handle Query Results DROP TEMPORARY TABLE IF EXISTS tblResults; CREATE TEMPORARY TABLE IF NOT EXISTS tblResults ( `category_id` int(11), `category` varchar(255), `parent_id` int(11), `sequence` int(11), `level` int(11), PRIMARY KEY (`category_id`) ); SET vLevel = 0; OPEN topCursor; TopLoop : LOOP -- Top Loop Begin FETCH topCursor INTO vCatID, vCatName, vParentID, vSequence; REPLACE INTO tblResults VALUES (vCatID, vCatName, vParentID, vSequence, vLevel); IF tDone THEN CLOSE topCursor; LEAVE TopLoop; END IF; OPEN subCursor; SubLoop : LOOP FETCH subCursor INTO vCatID, vCatName, vParentID, vSequence; REPLACE INTO tblResults VALUES (vCatID, vCatName, vParentID, vSequence, vLevel); IF tDone THEN SET vLevel = vLevel + 1; set tDone := false; CLOSE subCursor; LEAVE SubLoop; END IF; END LOOP SubLoop; END LOOP TopLoop; -- Top Loop End SELECT * FROM tblResults; END;