我需要一些帮助。

我的桌子是

CREATE TABLE `users` (
  `id` bigint(11) NOT NULL auto_increment,
  `point` bigint(11) NOT NULL,
  `lefth` bigint(11) NOT NULL,
  `righth` bigint(11) NOT NULL,
  `referel` bigint(11) NOT NULL,
  `dateadded` timestamp NOT NULL default CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=21 ;


数据

INSERT INTO `users` VALUES (1, 1000, 2, 4, 1, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (2, 0, 3, 5, 1, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (3, 0, 6, 9, 1, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (4, 0, 14, 7, 1, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (5, 0, 8, 0, 2, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (6, 0, 0, 0, 3, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (7, 0, 15, 19, 4, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (8, 0, 10, 11, 5, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (9, 0, 0, 0, 3, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (10, 0, 0, 0, 8, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (11, 0, 12, 13, 8, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (12, 0, 0, 0, 11, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (13, 0, 0, 0, 11, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (14, 0, 16, 17, 4, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (15, 0, 0, 0, 7, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (16, 0, 0, 0, 4, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (17, 0, 0, 18, 14, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (18, 0, 0, 0, 17, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (19, 0, 20, 0, 7, '0000-00-00 00:00:00');
INSERT INTO `users` VALUES (20, 0, 0, 0, 19, '0000-00-00 00:00:00');


我想编写一个过程,当提供一个值作为参数时,将返回表中传递的参数下的所有值。

像这样的东西

DELIMITER $$
--
CREATE DEFINER=`root`@`localhost` PROCEDURE `emp_heir`(
        IN param1 INTEGER(11)
    )
BEGIN
select lefth from users where id = param1;
END$$


这将仅返回lefth列中的值,但是如果lefth中的值具有子值(该id的lefth和righth中的值),则它也应返回该值。就像明智的做法一样,它应该持续到表格末尾。

如何做到这一点?

谢谢

最佳答案

CREATE DEFINER=`root`@`localhost` PROCEDURE `emp_heir` (IN param1 INTEGER(11), INOUT lResult VARCHAR(255), INOUT rResult VARCHAR(255))
BEGIN
    DECLARE lh INT(11);
    DECLARE rh INT(11);

    IF lResult IS NULL THEN
        SET lResult := '';
    END IF;
    IF rResult IS NULL THEN
        SET rResult := '';
    END IF;

    SELECT lefth, righth FROM users where id = param1 INTO lh, rh;

    IF lh != 0 THEN
        SET lResult := CONCAT(lResult, lh, ',');
    END IF;
    IF rh != 0 THEN
        SET rResult := CONCAT(rResult, rh, ',');
    END IF;

    IF lh != 0 THEN
        CALL emp_heir(lh, lResult, rResult);
    END IF;
    IF rh != 0 THEN
        CALL emp_heir(rh, lResult, rResult);
    END IF;
END$$


检查一下。去测试:

call emp_heir(2, @lresult, @rresult, 0);
select @lresult, @rresult;


只是很有趣,为什么不在应用程序中执行此操作? =)

UPD

如果对此出错,请执行

SET max_sp_recursion_depth = 10;


如果错误再次出现,请尝试增加该值

关于sql - 在创建过程时需要一些帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4131295/

10-09 22:24