如何从递归层次结构,父子关系mysql中找到位置

时间:2015-12-16 10:52:32

标签: php mysql recursion

我很累得到基于树的结果,这个结果在哪个位置。但是没有达到,也看到很多答案,如selectselect

这是我的表

CREATE TABLE `memberinfo` (
  `id` int(11) NOT NULL auto_increment,
  `parentid` int(11) NOT NULL default '0',
  `name` varchar(250) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;


id  parentid name
1     0      abc1
2     1      abc2
3     2      abc3
4     2      abc4
5     0      abc5
6     3      abc6

我想要结果位置如下:

id position
1      1
2      1-1
3      1-1-1
4      1-1-2
5      2
6      1-1-1-1

提前感谢您的帮助。

我尝试使用php递归,但速度太快,最后会出现致命错误(执行超时)

function getPosition($listparentid,$listid){

    $db = new dbfunction();
    $posNumber = '';
    do{
        $listresult = $db->rootPostionSearch($listparentid,$listid);
        $rowcount=mysqli_num_rows($listresult);
        if($rowcount > '0'){
            if ($listrow = mysqli_fetch_assoc($listresult)) {

                //switch to parent id
                $listid = $listrow['parentid'];

                //get next parent id
                $presult = $db->getRootParentInfo($listid);
                $pcount=mysqli_num_rows($presult);
                if($pcount > '0'){
                    if ($plistrow = mysqli_fetch_assoc($presult)) {
                        $listparentid = $plistrow['parentid'];
                    }
                }

                //get position
                $posNumber = $listrow['position']."-".$posNumber;
            }
        }

    }while($listparentid != '0');


    // get parent position number
    $listresult = $db->rootPostionSearch($listparentid,$listid);
    $rowcount=mysqli_num_rows($listresult);
    if($rowcount > '0'){
        if ($listrow = mysqli_fetch_assoc($listresult)) {
            //get position
            $posNumber = $listrow['position']."-".$posNumber;
        }
    }

    return $posNumber;
}


public function getRootParentInfo($id){
        $sql = "select parentid from memberinfo where id = $id";
        return $this->query($sql);
}

public function rootPostionSearch($rootpos,$pos){
        $sql = "SELECT x.id, x.parentid, x.position, x.name
            FROM 
            (
                SELECT t.id, t.parentid, t.name,@rownum := @rownum + 1 AS position
                FROM memberinfo t
                JOIN (SELECT @rownum := 0) r 
                where t.parentid = '$rootpos' order by id
            ) x where id = '$pos'";

        return $this->query($sql);
}

级别查询:

SELECT x.id, x.parentid, x.position, x.name
        FROM 
        (
            SELECT t.id, t.parentid, t.name,@rownum := @rownum + 1 AS position
            FROM memberinfo t
            JOIN (SELECT @rownum := 0) r 
            where t.parentid = '0' order by id
        ) x

1 个答案:

答案 0 :(得分:0)

我展示了与@RolandoMySQLDBA回答相关的问题,我做了一些小改动,将parent_id麻痹到了memberinfo表中。 我帮助你:

DELIMITER $$
DROP FUNCTION IF EXISTS `GetAncestry` $$
CREATE FUNCTION `GetAncestry` (GivenID INT) RETURNS VARCHAR(1024)
DETERMINISTIC
BEGIN
    DECLARE rv VARCHAR(1024);
    DECLARE cm CHAR(1);
    DECLARE ch INT;
    DECLARE cid INT;
    DECLARE label VARCHAR(24);


    SET rv = '';
    SET cm = '';
    SET ch = GivenID;
    SET cid = 0;
    SET label = '';
    WHILE ch > 0 DO
    SET cid = ch;
        SELECT IFNULL(parent_id,-1) INTO ch FROM
        (SELECT * FROM pctable WHERE id = ch) A;

    SELECT pos INTO label FROM
        (

SELECT x.id, x.parent_id, x.pos
FROM 
(
SELECT t.id, t.parent_id, @rownum := @rownum + 1 AS pos
FROM pctable t
JOIN (SELECT @rownum := 0) r 
where t.parent_id = ch order by id
) x where x.id = cid

    ) P;
        IF ch > 0 THEN
            SET rv = CONCAT(rv,cm,label);
            SET cm = '-';
    else
            SET rv = CONCAT(rv,cm,label);
        END IF;
    END WHILE;
    RETURN rv;
END $$
DELIMITER ;

在这里打电话:

select id,GetAncestry(id) from memberinfo

并且回答如下:

id position
1      1
2      1-1
3      1-1-1
4      1-1-2
5      2
6      1-1-1-1