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

标签 php mysql recursion

我非常厌倦获取基于树的结果,即哪个 id 在哪个位置。但没有达到,也看到很多答案如selectselect

这是我的 table

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 递归,但速度太慢,最后出现 fatal error (执行超时)

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

最佳答案

我显示您与 @RolandoMySQLDBA 相关的问题答案我做了一些小改动,将parentid 更改为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

关于php - 如何从递归层次结构中查找位置,父子关系mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34310068/

相关文章:

php - 面向对象和过程的 Mysqli 帮助?

通过 ssh 隧道的 PhpStorm XDebug

python - Django:将行插入数据库

mysql - 在 MySQL 中按表计算每日行数

javascript - 查找一个数字的所有排列而不重复(为清楚起见进行了编辑)

php - 返回是或否 PHP 查询

php - 带有 pdo 的基于文件的 php 数据库

c - 如何使用递归查找数字中的最小元素 [C]

mysql - 如何实现基于同义词的上下文搜索?

c++ - 使用递归后程序爆炸