在创建程序时需要一些帮助

时间:2010-11-09 07:20:03

标签: sql mysql stored-procedures

我需要一些帮助。

我的桌子是

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中的值),则它也应返回该值。就像明智一样,它应该持续到表的末尾。

如何做到这一点?

由于

3 个答案:

答案 0 :(得分:1)

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;

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

答案 1 :(得分:0)

这是你想要的吗?

DELIMITER $$

CREATE PROCEDURE my(
    IN number INT,   
    OUT lefte INT,
    OUT righte INT

)
BEGIN
   SELECT lefth, righth 
   FROM users WHERE id = number INTO lefte , righte;

END;

$$

DELIMITER ;
这个电话就像是:

CALL my(4,@lefth,@righth);
SELECT @lefth,@righth;

答案 2 :(得分:0)

CREATE DEFINER=`root`@`localhost` PROCEDURE `emp_heir` (IN param1 INTEGER(11))
BEGIN
    DECLARE lh INT(11);
    DECLARE rh INT(11);
    DECLARE lresult VARCHAR(255) default '';
    DECLARE rresult VARCHAR(255) default '';
    SET lh := param1;
    SET rh := param1;
    REPEAT
        SELECT lefth FROM users WHERE id = lh INTO lh;
        SET lresult := CONCAT(lresult, lh, ',');
    UNTIL lh = 0 END REPEAT;
    REPEAT
        SELECT righth FROM users WHERE id = rh INTO rh;
        SET rresult := CONCAT(rresult, rh, ',');
    UNTIL rh = 0 END REPEAT;
    SELECT LEFT(lresult, LENGTH(lresult) - 3) AS lefths, LEFT(rresult, LENGTH(rresult) - 3) AS righths;
END$$
你有类似的东西吗?此过程以递归方式获取所有lefthrighth值,并将其放入单独的字符串中。